zoukankan      html  css  js  c++  java
  • java实现httpclient2

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    
    public class TTT
    {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 * @throws ClientProtocolException 
    	 */
    	public static void main(String[] args) throws ClientProtocolException, IOException
    	{
    		// 创建HttpClient实例   
    	    HttpClient httpclient = new DefaultHttpClient();
    	    // 创建Get方法实例   
            HttpGet httpgets = new HttpGet("http://127.0.0.1/testhttp.php?username=yonghuming");  
            HttpResponse response = httpclient.execute(httpgets);  
            HttpEntity entity = response.getEntity();  
            if (entity != null) {  
                InputStream instreams = entity.getContent();  
                String str = convertStreamToString(instreams);
                System.out.println("Do something"); 
                System.out.println(str);
                // Do not need the rest  
                httpgets.abort();  
            }
    	}
    	
    	public static String convertStreamToString(InputStream is) {    
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));    
            StringBuilder sb = new StringBuilder();    
         
            String line = null;    
            try {    
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "
    ");    
                }    
            } catch (IOException e) {    
                e.printStackTrace();    
            } finally {    
                try {    
                    is.close();    
                } catch (IOException e) {    
                   e.printStackTrace();    
                }    
            }    
            return sb.toString();    
        }
    
    }
    
  • 相关阅读:
    cocos2d-x 获得系统语言繁体
    状态机
    cocos2d-x 混合模式
    cocos2d-x 3.x 橡皮擦功能
    MySQL 库大小、表大小、索引大小查询命令
    MySQL批量杀进程
    多实例MySQL批量添加用户和密码并授权
    删除或清空具有外键约束的表数据报-ERROR 1701 (42000)
    ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
    MySQL 多实例给root用户创建密码
  • 原文地址:https://www.cnblogs.com/lgm1999/p/5754927.html
Copyright © 2011-2022 走看看