package com.yeshi.buwan.util.tb;
|
|
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 java.io.*;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
public class TaoBaoHttpUtil {
|
|
|
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);
|
|
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, 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) {
|
// 聚石塔服务器环境 118.178.179.189
|
|
// result = get("http://118.178.179.189/taoke/", params, false);
|
// if (StringUtil.isNullOrEmpty(result))
|
//https://eco.taobao.com/router/rest
|
//http://gw.api.taobao.com/router/rest
|
String result = get("https://eco.taobao.com/router/rest", params, false);
|
return result;
|
}
|
|
public static String get(String url, Map<String, String> 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();
|
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();
|
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, Map<String, String> map) {
|
Iterator<String> 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.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<String, String> map, Map<String, String> headers) {
|
org.apache.http.client.HttpClient client = new DefaultHttpClient();
|
|
HttpPost httpRequst = new HttpPost(url);// 创建HttpPost对象
|
List<org.apache.http.NameValuePair> params = new ArrayList<org.apache.http.NameValuePair>();
|
Iterator<String> 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<String> 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();
|
|
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 "";
|
}
|
}
|