package com.ks.lijin.utils.taobao;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.net.URLEncoder;
|
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.methods.GetMethod;
|
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
|
|
|
public class TaoBaoHttpUtil {
|
|
|
public static String proxyGet(String url, String cookie) {
|
HttpClient client = new HttpClient();
|
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<String, String> headers) {
|
HttpClient client = new HttpClient();
|
try {
|
GetMethod method = new GetMethod(url);
|
if (headers != null) {
|
Iterator<String> 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, Map<String, String> params, boolean proxy) {
|
HttpClient client = new HttpClient();
|
// client.getHostConfiguration().setProxy("192.168.1.122", 8888);
|
try {
|
Iterator<String> 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<String, String> params, Map<String, String> headers, boolean proxy) {
|
HttpClient client = new HttpClient();
|
try {
|
Iterator<String> 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<String, String> params) {
|
String result = get("https://eco.taobao.com/router/rest", params, false);
|
return result;
|
}
|
|
public static String get(String url, Map<String, String> params) {
|
return get(url, params, false);
|
}
|
|
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;
|
}
|
|
|
|
|
|
|
|
|
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();
|
}
|
|
|
|
}
|