java 简单的HttpClient工具类,解决返回中文有部分乱码的情况



java 简单的HttpClient工具类,解决返回中文有部分乱码的情况。

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.client.ClientProtocolException;
  6. import org.apache.http.client.HttpClient;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. public class HttpClientUtil {
  10.     public static String sendGet(String url,String data) throws ClientProtocolException, IOException
  11.     {
  12.         // 创建HttpClient实例
  13.         HttpClient httpclient = new DefaultHttpClient();
  14.         // 创建Get方法实例
  15.         HttpGet httpgets = new HttpGet(url+data);
  16.         HttpResponse response = httpclient.execute(httpgets);
  17.         HttpEntity entity = response.getEntity();
  18.         if (entity != null) {
  19.             InputStream instreams = entity.getContent();
  20.             String str = convertStreamToString(instreams);
  21.             httpgets.abort();
  22.             return str;
  23.         }
  24.         return null;
  25.     }
  26.     public static String convertStreamToString(InputStream is) {
  27.         StringBuilder sb1 = new StringBuilder();
  28.         byte[] bytes = new byte[4096];
  29.         int size = 0;
  30.         try {
  31.             while ((size = is.read(bytes)) > 0) {
  32.                 String str = new String(bytes, 0, size, “UTF-8″);
  33.                 sb1.append(str);
  34.             }
  35.         } catch (IOException e) {
  36.             e.printStackTrace();
  37.         } finally {
  38.             try {
  39.                 is.close();
  40.             } catch (IOException e) {
  41.                e.printStackTrace();
  42.             }
  43.         }
  44.         return sb1.toString();
  45.     }
  46. }
import java.io.IOException;
import java.io.InputStream;

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 HttpClientUtil {
	public static String sendGet(String url,String data) throws ClientProtocolException, IOException
	{
		// 创建HttpClient实例   
	    HttpClient httpclient = new DefaultHttpClient();
	    // 创建Get方法实例   
        HttpGet httpgets = new HttpGet(url+data);  
        HttpResponse response = httpclient.execute(httpgets);  
        HttpEntity entity = response.getEntity();  
        if (entity != null) {  
            InputStream instreams = entity.getContent();  
            String str = convertStreamToString(instreams);
            httpgets.abort();  
            return str;
        }
        return null;
	}

	public static String convertStreamToString(InputStream is) {    
        StringBuilder sb1 = new StringBuilder();    
        byte[] bytes = new byte[4096];  
        int size = 0;  

        try {    
        	while ((size = is.read(bytes)) > 0) {  
                String str = new String(bytes, 0, size, "UTF-8");  
                sb1.append(str);  
            }  
        } catch (IOException e) {    
            e.printStackTrace();    
        } finally {    
            try {    
                is.close();    
            } catch (IOException e) {    
               e.printStackTrace();    
            }    
        }    
        return sb1.toString();    
    }
}

是完整代码。