public class HttpTool {
static int Timeout = 10000;
static int RetryTime = 3;
private DefaultHttpClient httpclient;
public HttpTool() {
httpclient = new DefaultHttpClient();
setTimeoutRetry(Timeout);
// httphost
}
void setTimeoutRetry(int time) {
httpclient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, time);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
time);
DefaultHttpRequestRetryHandler dhr = new DefaultHttpRequestRetryHandler(
RetryTime, true);
httpclient.setHttpRequestRetryHandler(dhr);
}
void cancelTimeoutRetry() {
httpclient.getParams().removeParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT);
}
/*
* Example:添加POST的内容 List<NameValuePair> nvps = new
* ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair(key,value));
*/
// 已废弃
public String http_post(String url, Header[] headers,
List<NameValuePair> postpair) {
String responseString = null;
boolean status = false;
while (status == false) {
HttpPost post = new HttpPost(url);
HttpResponse response = null;
// 添加header
for (Header header : headers)
post.addHeader(header);
try {
post.setEntity(new UrlEncodedFormEntity(postpair, HTTP.UTF_8));
response = httpclient.execute(post);
responseString = EntityUtils.toString(response.getEntity());
status = true;
} catch (ConnectTimeoutException e) {
e.printStackTrace();
System.out.println("ConnectTimeout Caught");
status = false;
} catch (SocketTimeoutException e) {
e.printStackTrace();
System.out.println("SocketTimeout Caught");
status = false;
} catch (NoHttpResponseException e) {
e.printStackTrace();
System.out.println("NoResponse Caught");
status = false;
} catch (UnknownHostException e) {
System.out.println("unknownhost Caught");
e.printStackTrace();
try {
Thread.sleep(1000);
} catch (Exception e1) {
;
}
status = false;
} catch (SocketException e) {
e.printStackTrace();
try {
Thread.sleep(1000);
} catch (Exception e1) {
;
}
System.out.println("SocketException Caught");
status = false;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
post.abort();
}
}
return responseString;
}
public String http_post(String url, Header[] headers, String JsonStr) {
String responseString = null;
boolean status = false;
while (status == false) {
HttpPost post = new HttpPost(url);
HttpResponse response = null;
// 添加header
for (Header header : headers)
post.addHeader(header);
try {
post.setEntity(new StringEntity(JsonStr, HTTP.UTF_8));
response = httpclient.execute(post);
responseString = EntityUtils.toString(response.getEntity());
status = true;
} catch (ConnectTimeoutException e) {
e.printStackTrace();
System.out.println("ConnectTimeout Caught");
status = false;
} catch (SocketTimeoutException e) {
e.printStackTrace();
System.out.println("SocketTimeout Caught");
status = false;
} catch (NoHttpResponseException e) {
e.printStackTrace();
System.out.println("NoResponse Caught");
status = false;
} catch (UnknownHostException e) {
System.out.println("unknownhost Caught");
e.printStackTrace();
try {
Thread.sleep(1000);
} catch (Exception e1) {
;
}
status = false;
} catch (SocketException e) {
e.printStackTrace();
try {
Thread.sleep(1000);
} catch (Exception e1) {
;
}
System.out.println("SocketException Caught");
status = false;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
post.abort();
}
}
return responseString;
}
public String http_post(String url, List<NameValuePair> postpair) {
return http_post(url, null, postpair);
}
public String http_get(String url) {
return http_get(url, null);
}
public String http_get(String url, Header[] headers) {
String responseString = null;
boolean status = false;
while (status == false) {
HttpGet post = new HttpGet(url);
HttpResponse response = null;
// 添加header
for (Header header : headers)
post.addHeader(header);
try {
response = httpclient.execute(post);
responseString = EntityUtils.toString(response.getEntity());
System.out.println(responseString);
status = true;
} catch (ConnectTimeoutException e) {
e.printStackTrace();
System.out.println("ConnectTimeout Caught");
status = false;
} catch (SocketTimeoutException e) {
e.printStackTrace();
System.out.println("SocketTimeout Caught");
status = false;
} catch (NoHttpResponseException e) {
e.printStackTrace();
System.out.println("NoResponse Caught");
status = false;
} catch (UnknownHostException e) {
System.out.println("unknownhost Caught");
e.printStackTrace();
try {
Thread.sleep(1000);
} catch (Exception e1) {
;
}
status = false;
} catch (SocketException e) {
e.printStackTrace();
try {
Thread.sleep(1000);
} catch (Exception e1) {
;
}
System.out.println("SocketException Caught");
status = false;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
post.abort();
}
}
// return response.toString().contains("200 OK");
return responseString;
}
/*
* HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
*
* public boolean retryRequest( IOException exception, int executionCount,
* HttpContext context) { if (executionCount >= 5) { // Do not retry if over
* max retry count return false; } if (exception instanceof
* NoHttpResponseException) { // Retry if the server dropped connection on
* us return true; } if (exception instanceof SSLHandshakeException) { // Do
* not retry on SSL handshake exception return false; } HttpRequest request
* = (HttpRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST);
* boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if
* (idempotent) { // Retry if the request is considered idempotent return
* true; } return false; }
*
* };
*/
}
//用于生成post键值对
class PostParaFactory {
List<NameValuePair> nvps;
PostParaFactory thisclass;
public PostParaFactory() {
nvps = new ArrayList<NameValuePair>();
}
public PostParaFactory(BasicNameValuePair... bnvps) {
this();
for(BasicNameValuePair bnvp:bnvps)
nvps.add(bnvp);
}
public PostParaFactory addPara(String key, String value) {
nvps.add(new BasicNameValuePair(key, value));
return thisclass;
}
public List<NameValuePair> getParas() {
return nvps;
}
public String toJSON() {
Gson gson = new Gson();
return gson.toJson(nvps);
}
}
//用于生成http头
class HeaderFactory {
List<Header> headerlist;
Header[] headers;
HeaderFactory() {
headerlist = new ArrayList<Header>();
headers = new Header[1];
}
public HeaderFactory addCookie(String arg) {
headerlist.add(new BasicHeader("Cookie", arg));
return this;
}
public HeaderFactory addAuthorization(String arg) {
headerlist.add(new BasicHeader("Authorization", arg));
return this;
}
public HeaderFactory addContentType(String arg) {
headerlist.add(new BasicHeader("Content-Type", arg));
return this;
}
public Header[] getHeaders() {
// headers = new Header[headerlist.size()];
return headerlist.toArray(headers);
}
}