package com.yeshi.fanli.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.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; 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; import org.apache.commons.httpclient.params.HttpConnectionManagerParams; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.yeshi.fanli.entity.address.Address; import com.yeshi.fanli.util.taobao.TaoBaoOrderUtil; import net.sf.json.JSONArray; public class TaoBaoHttpUtil { public static String getShortLink(String url) { String totalUrl = String.format("http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=%s", URLEncoder.encode(url)); String result = get(totalUrl); JSONArray rtesultArray = JSONArray.fromObject(result); if (rtesultArray.size() > 0) { return rtesultArray.optJSONObject(0).optString("url_short"); } return null; } public static String proxyGet(String url, String cookie) { HttpClient client = new HttpClient(); // Address address = ProxyUtil.getAddressProxy(); // if(address != null){ // client.getHostConfiguration().setProxy(address.getIp(), // address.getPort()); // } try { GetMethod method = new GetMethod(url); if (cookie != null) { method.addRequestHeader("cookie", cookie); } client.executeMethod(method); return method.getResponseBodyAsString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String proxyGet(String url, Map headers) { HttpClient client = new HttpClient(); // Address address = ProxyUtil.getAddressProxy(); // if(address != null){ // client.getHostConfiguration().setProxy(address.getIp(), // address.getPort()); // } try { GetMethod method = new GetMethod(url); if (headers != null) { Iterator keys = headers.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); method.addRequestHeader(key, headers.get(key)); } } client.executeMethod(method); return method.getResponseBodyAsString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String get(String url) { return get(url, true); } public static String get(String url, boolean isProxy) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url); if (isProxy) { Address address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } } try { client.executeMethod(method); return convertInputStreamToString(method.getResponseBodyAsStream()); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String getAsString(String url, boolean isProxy) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url); if (isProxy) { Address address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } } try { client.executeMethod(method); String response = method.getResponseBodyAsString(); String s1 = new String(response.getBytes("ISO-8859-1"), "UTF-8"); return s1; } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String tbGet(String url, Map params, String tbAccount) { HttpClient client = new HttpClient(); Address address = ProxyUtil.getAddressProxy(); // if(address != null){ // client.getHostConfiguration().setProxy(address.getIp(), // address.getPort()); // } Iterator keys = params.keySet().iterator(); url += "?"; while (keys.hasNext()) { String key = keys.next(); try { url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } GetMethod method = new GetMethod(url); try { method.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"); method.setRequestHeader("Upgrade-Insecure-Requests", "1"); method.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); method.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8"); method.setRequestHeader("Cache-Control", "max-age=0"); String taoBaoCookie = TaoBaoOrderUtil.getTaoBaoCookie(tbAccount); method.setRequestHeader("cookie", taoBaoCookie); // HttpConnectionManagerParams params2 = new // HttpConnectionManagerParams(); // params2.setConnectionTimeout(3500); // client.getHttpConnectionManager().setParams(params2); client.executeMethod(method); // InputStream inputStream = method.getResponseBodyAsStream(); // String result = convertInputStreamToString(inputStream); return method.getResponseBodyAsString();// convertInputStreamToString(method.getResponseBodyAsStream()); } catch (Exception e) { try { address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } client.executeMethod(method); } catch (Exception e1) { e1.printStackTrace(); } } return ""; } public static String get(String url, Map params, boolean proxy) { HttpClient client = new HttpClient(); // client.getHostConfiguration().setProxy("192.168.1.122", 8888); try { Iterator keys = params.keySet().iterator(); url += "?"; while (keys.hasNext()) { String key = keys.next(); url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8")); } // System.out.println("url"+url); GetMethod method = new GetMethod(url); // 3S的响应超时 HttpConnectionManagerParams hparams = new HttpConnectionManagerParams(); hparams.setConnectionTimeout(3000); client.getHttpConnectionManager().setParams(hparams); /* * if(proxy){ Address address = ProxyUtil.getAddressProxy(); * if(address != null){ HttpConnectionManagerParams hparams = new * HttpConnectionManagerParams(); * hparams.setConnectionTimeout(3500); * client.getHttpConnectionManager().setParams(hparams); * client.getHostConfiguration().setProxy(address.getIp(),address. * getPort()); } } */ // client.getHostConfiguration().setProxy("192.168.1.122",8888); client.executeMethod(method); String result = method.getResponseBodyAsString(); return result; } catch (Exception e) { e.printStackTrace(); } return ""; } public static String get(String url, Map params, Map headers, boolean proxy) { HttpClient client = new HttpClient(); try { Iterator keys = params.keySet().iterator(); url += "?"; while (keys.hasNext()) { String key = keys.next(); url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8")); } System.out.println(url); GetMethod method = new GetMethod(url); if (headers != null) { keys = headers.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); headers.get(key); method.setRequestHeader(key, headers.get(key)); } } /* * if(proxy){ Address address = ProxyUtil.getAddressProxy(); * if(address != null){ HttpConnectionManagerParams hparams = new * HttpConnectionManagerParams(); * hparams.setConnectionTimeout(3500); * client.getHttpConnectionManager().setParams(hparams); * client.getHostConfiguration().setProxy(address.getIp(),address. * getPort()); } } */ // client.getHostConfiguration().setProxy("120.92.118.64", 10000); client.executeMethod(method); String result = method.getResponseBodyAsString(); return result; } catch (Exception e) { e.printStackTrace(); } return ""; } public static String taoKeGet(Map params) { // 聚石塔服务器环境 118.178.179.189 // result = get("http://118.178.179.189/taoke/", params, false); // if (StringUtil.isNullOrEmpty(result)) String result = get("http://gw.api.taobao.com/router/rest", params, false); return result; } public static String get(String url, Map params) { // TODO 淘宝的所有请求需要走聚石塔 // http://118.178.179.189/taoke/ return get(url, params, false); } public static String get(String url, String charset) { HttpClient client = new HttpClient(); Address address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } 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(); 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(); Address address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } 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(); Address address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } 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, 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(); Address address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } 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(params); 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, Map headers) { org.apache.http.client.HttpClient client = new DefaultHttpClient(); HttpPost httpRequst = new HttpPost(url);// 创建HttpPost对象 List params = new ArrayList(); Iterator its = map.keySet().iterator(); while (its.hasNext()) { String key = its.next(); params.add(new BasicNameValuePair(key, map.get(key))); } try { HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8"); httpRequst.setEntity(entity); HttpResponse httpResponse = client.execute(httpRequst); if (httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = httpResponse.getEntity(); String result = EntityUtils.toString(httpEntity, "UTF-8");// 取出应答字符�? return result; } } catch (Exception e) { e.printStackTrace(); } return ""; // Iterator its = map.keySet().iterator(); } 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(); Address address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } 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(); Address address = ProxyUtil.getAddressProxy(); if (address != null) { client.getHostConfiguration().setProxy(address.getIp(), address.getPort()); } 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 ""; } }