package com.whty.sub.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* HTTP CLIENT 工具类
*
* @author cjq
*/
public class HttpClientUtil {
private static final String ENCODE_CHARSET_UTF8 = "UTF-8";
/**
*@Title: get
*@Description: TODO(模拟firefox浏览器插件poster的get方式请求)
*@Author: hxf
*@param getUrl
* 请求服务地址
*@param msTimeout
* 超时时间
*@param ip
* 被请求服务的IP地址
*@param port
* 被请求服务的端口号
*@param user
* 鉴权通过的用户名
*@param password
* 用户密码
*@return String
*/
public static String get(String getUrl,Integer msTimeout,String ip,int port,String user,String password) throws Exception{
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager ();
// 设置超时时间
connectionManager.getParams ().setConnectionTimeout (msTimeout);
HttpClient httpClient = new HttpClient (connectionManager);
// 设置被请求服务的ip和端口
httpClient.getHostConfiguration ().setHost (ip, port);
GetMethod postMethod = new GetMethod (getUrl);
httpClient.getParams ().setAuthenticationPreemptive (true);
// 设置鉴权通过的用户名和密码
httpClient.getState ().setProxyCredentials (AuthScope.ANY, new UsernamePasswordCredentials (user,password));
try {
List<Header> headers = new ArrayList<Header> ();
headers.add (new Header ("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
headers.add (new Header ("Authorization","Basic ZG9tYWluX3VzZXI6ZG9tYWluX3VzZXI="));
headers.add (new Header ("Accept-Encoding","gzip, deflate"));
headers.add (new Header ("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"));
headers.add (new Header ("Connection","keep-alive"));
headers.add (new Header ("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1"));
httpClient.getHostConfiguration ().getParams ().setParameter ("http.default-headers", headers);
postMethod.getParams ().setParameter (HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler (1,false));
postMethod.getParams ().setParameter (HttpMethodParams.HTTP_CONTENT_CHARSET, ENCODE_CHARSET_UTF8);
postMethod.getParams ().setParameter (HttpMethodParams.HTTP_URI_CHARSET, ENCODE_CHARSET_UTF8);
postMethod.getParams ().setParameter (HttpMethodParams.SO_TIMEOUT, msTimeout);
Integer status = httpClient.executeMethod (postMethod);
if (status == 200) {
return postMethod.getResponseBodyAsString ();
} else {
throw new Exception (String.format ("Invoke remote server address %1$s error, return status = %2$s!", getUrl, status));
}
} catch (HttpException e) {
throw new Exception (String.format ("Invoke remote server address %1$s error!", getUrl),e);
} catch (IOException e) {
throw new Exception (String.format ("Invoke remote server address %1$s error!", getUrl),e);
} finally {
postMethod.releaseConnection ();
}
}
@SuppressWarnings("deprecation")
public static String post(String requeString,String postUrl,Integer msTimeout) throws Exception{
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager ();
connectionManager.getParams ().setConnectionTimeout (msTimeout);
HttpClient httpClient = new HttpClient (connectionManager);
PostMethod postMethod = new PostMethod (postUrl);
httpClient.getState ().setProxyCredentials (AuthScope.ANY, new UsernamePasswordCredentials ("domain_user","domain_user"));
try {
List<Header> headers = new ArrayList<Header> ();
headers.add (new Header ("Content-Type","text/xml; charset=" + ENCODE_CHARSET_UTF8));
httpClient.getHostConfiguration ().getParams ().setParameter ("http.default-headers", headers);
postMethod.setRequestBody (requeString);
postMethod.getParams ().setParameter (HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler (1,false));
postMethod.getParams ().setParameter (HttpMethodParams.HTTP_CONTENT_CHARSET, ENCODE_CHARSET_UTF8);
postMethod.getParams ().setParameter (HttpMethodParams.HTTP_URI_CHARSET, ENCODE_CHARSET_UTF8);
postMethod.getParams ().setParameter (HttpMethodParams.SO_TIMEOUT, msTimeout);
Integer status = httpClient.executeMethod (postMethod);
if (status == 200) {
return postMethod.getResponseBodyAsString ();
} else {
throw new Exception (String.format ("Invoke remote server address %1$s error, return status = %2$s!", postUrl, status));
}
} catch (HttpException e) {
throw new Exception (String.format ("Invoke remote server address %1$s error!", postUrl),e);
} catch (IOException e) {
throw new Exception (String.format ("Invoke remote server address %1$s error!", postUrl),e);
} finally {
postMethod.releaseConnection ();
}
}
}