package com.jiuqi.njt.data; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.zip.GZIPInputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; /** * * <p>TODO 网络请求辅助类</p> * * <p>Copyright: 版权所有 (c) 2002 - 2008<br> * Company: 久其</p> * * @author jiangxuelei * @version 2012-7-24 */ public class HttpHelper { private static HttpClient imageclient; @SuppressWarnings("unused") private static synchronized HttpClient getImageclient() { if(imageclient==null){ imageclient=CustomerHttpClient.getImageHttpClient(); } return imageclient; } public InputStream openStream(String url) throws IllegalStateException, IOException{ HttpClient client =CustomerHttpClient.getImageHttpClient(); if(client ==null){ return null; } HttpResponse response = client.execute(getHttpPost(url, null)); if (response.getStatusLine().getStatusCode() != 200) { return null; } HttpEntity entity = response.getEntity(); InputStream content; if(entity.getContentEncoding()!=null&& entity.getContentEncoding().getValue().equals("gzip")){ content = new GZIPInputStream(entity.getContent()); }else{ content = entity.getContent(); } return content; } public String getEntity(String url, List<? extends NameValuePair> parameters) throws IOException { return getResponse(getHttpPost(url, new UrlEncodedFormEntity(parameters,"GBK"))); } public String getEntity(String url, String argumentsExpression) throws IOException { return getResponse(getHttpPost(url, new StringEntity(argumentsExpression,"GBK"))); } public String getEntity(String url) throws ClientProtocolException, IOException { return getResponse(getHttpPost(url, null)); } public String getEntity(String url, String argumentsExpression,boolean get) throws IOException { if(!get)return getResponse(getHttpPost(url, new StringEntity(argumentsExpression,"GBK"))); else{ HttpClient client = CustomerHttpClient.getHttpClient(); HttpGet getter=new HttpGet(url+argumentsExpression); HttpResponse response = client.execute(getter); if (response.getStatusLine().getStatusCode() != 200) { return null; } HttpEntity entity = response.getEntity(); InputStream content; if(entity.getContentEncoding()!=null&& entity.getContentEncoding().getValue().equals("gzip")){ content = new GZIPInputStream(entity.getContent()); }else{ content = entity.getContent(); } String returnConnection = convertStreamToString(content, "GBK"); return returnConnection; } } private HttpPost getHttpPost(String url,HttpEntity entity){ String murl=url; HttpPost httpPost = new HttpPost(murl); httpPost.addHeader("Accept-Encoding","gzip"); if(entity!=null)httpPost.setEntity(entity); return httpPost; } private String getResponse(HttpPost httpPost) throws IllegalStateException, IOException{ HttpClient client = CustomerHttpClient.getHttpClient(); HttpResponse response = client.execute(httpPost); if (response.getStatusLine().getStatusCode() != 200) { return null; } HttpEntity entity = response.getEntity(); InputStream content; if(entity.getContentEncoding()!=null&& entity.getContentEncoding().getValue().equals("gzip")){ content = new GZIPInputStream(entity.getContent()); }else{ content = entity.getContent(); } String returnConnection = convertStreamToString(content, "GBK"); return returnConnection; } private String convertStreamToString(InputStream is, String Encoding) { BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader(is, "GBK")); } catch (UnsupportedEncodingException e1) { 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(); } } System.out.println(sb.toString()); return sb.toString(); } public static String replaceHttpGetParam(String str) { return str.replace("\"", "%22").replace("{", "%7b").replace("}", "%7d"); //特殊字符进行转义 } }