package com.yeshi.buwan.util; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Iterator; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; public class HttpUtil { public static String get(String url) { HttpClient client = new HttpClient(); // client.getHostConfiguration().setProxy("192.168.1.200", 8888); GetMethod method = new GetMethod(url); try { client.executeMethod(method); method.getStatusCode(); return convertInputStreamToString(method.getResponseBodyAsStream()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String defaultGet(String url) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url); try { client.executeMethod(method); method.getStatusCode(); return method.getResponseBodyAsString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String get(String url, Map map) { HttpClient client = new HttpClient(); // client.getHostConfiguration().setProxy("192.168.1.122", 8888); String totalUrl = url; totalUrl += "?"; Iterator its = map.keySet().iterator(); while (its.hasNext()) { String key = its.next(); String value = map.get(key); totalUrl += key + "=" + value + "&"; } if (totalUrl.endsWith("&")) totalUrl = totalUrl.substring(0, totalUrl.length() - 1); GetMethod method = new GetMethod(totalUrl); try { client.executeMethod(method); return convertInputStreamToString(method.getResponseBodyAsStream()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String get(String url, String charset) { HttpClient client = new HttpClient(); // client.getHostConfiguration().setProxy("192.168.1.122", 8888); GetMethod method = new GetMethod(url); try { client.executeMethod(method); BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), "ISO-8859-1")); String tmp = null; String htmlRet = ""; while ((tmp = reader.readLine()) != null) { htmlRet += tmp + "\r\n"; } return new String(htmlRet.getBytes(charset), "UTF-8"); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static InputStream getAsInputStream(String url) { HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); GetMethod method = new GetMethod(url); try { client.executeMethod(method); return method.getResponseBodyAsStream(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public static InputStream getImage(String url) { HttpClient client = new HttpClient(); // client.getHostConfiguration().setProxy("192.168.1.122", 8888); GetMethod method = new GetMethod(url); try { method.setRequestHeader("Content-Type", "image/jpeg"); client.executeMethod(method); return method.getResponseBodyAsStream(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public static String post(String url) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); method.addRequestHeader("Content-Type", "text/html;charset=UTF-8"); method.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); try { client.executeMethod(method); return convertInputStreamToString(method.getResponseBodyAsStream()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String post(String url,String entity,Map headers) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); if(headers!=null) for(String key:headers.keySet()){ method.addRequestHeader(key, headers.get(key)); } method.setRequestBody(entity); try { client.executeMethod(method); return convertInputStreamToString(method.getResponseBodyAsStream()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String post(String url, Map map) { Iterator its = map.keySet().iterator(); NameValuePair[] params = new NameValuePair[map.keySet().size()]; int p = 0; while (its.hasNext()) { String key = its.next(); NameValuePair np = new NameValuePair(key, map.get(key)); params[p] = np; p++; } HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); method.setRequestBody(params); try { client.executeMethod(method); return convertInputStreamToString(method.getResponseBodyAsStream()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } private static String convertInputStreamToString(InputStream inputStream) { BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer stringBuffer = new StringBuffer(); String str = ""; try { while ((str = br.readLine()) != null) { stringBuffer.append(str); } } catch (IOException e) { e.printStackTrace(); } return stringBuffer.toString(); } @SuppressWarnings("deprecation") public static String post(String url, String entity) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); method.addRequestHeader("Content-Type", "text/html;charset=UTF-8"); method.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); method.setRequestBody(entity); try { client.executeMethod(method); return convertInputStreamToString(method.getResponseBodyAsStream()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String postInputStream(String url, File file) throws FileNotFoundException { HttpClient client = new HttpClient(); PostMethod postMethod = new PostMethod(url); /* * postMethod.addRequestHeader("Content-Type", * "text/html;charset=UTF-8"); * postMethod.setRequestHeader("Content-Type", * "text/html;charset=UTF-8"); */ Part[] parts = { new StringPart("filename", file.getName()), new StringPart("filelength", file.length() + ""), new StringPart("content-type", "image/jpg"), new FilePart("file", file) }; postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); try { client.executeMethod(postMethod); return convertInputStreamToString(postMethod.getResponseBodyAsStream()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } }