Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.frameworkset.spi.remote.http.HttpRequestProxy Maven / Gradle / Ivy
Go to download
bboss is a j2ee framework include aop/ioc,mvc,persistent,taglib,rpc,event ,bean-xml serializable and so on.http://www.bbossgroups.com
package org.frameworkset.spi.remote.http;
import com.frameworkset.util.SimpleStringUtil;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.frameworkset.spi.remote.http.proxy.*;
import org.frameworkset.util.ResourceStartResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.net.NoRouteToHostException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import static org.frameworkset.spi.remote.http.HttpRequestUtil.object2json;
/**
* @author yinbp
* @Date:2016-11-20 11:39:59
*/
public class HttpRequestProxy {
private static final Logger logger = LoggerFactory.getLogger(HttpRequestProxy.class);
public static ResourceStartResult startHttpPools(String configFile){
return HttpRequestUtil.startHttpPools(configFile);
}
public static ResourceStartResult startHttpPools(Map configs){
return HttpRequestUtil.startHttpPools(configs);
}
public static ResourceStartResult startHttpPoolsFromApollo(String namespaces){
return HttpRequestUtil.startHttpPoolsFromApollo(namespaces);
}
public static ResourceStartResult startHttpPoolsFromApollo(String namespaces,String configChangeListener){
return HttpRequestUtil.startHttpPoolsFromApollo(namespaces,configChangeListener);
}
public static ResourceStartResult startHttpPoolsFromApolloAwaredChange(String namespaces){
return HttpRequestUtil.startHttpPoolsFromApolloAwaredChange(namespaces);
}
public static ResourceStartResult startHttpPoolsFromNacos(String namespace, String serverAddr, String dataId, String group, long timeOut,Map pros){
return HttpRequestUtil.startHttpPoolsFromNacos( namespace, serverAddr, dataId, group, timeOut, pros);
}
public static ResourceStartResult startHttpPoolsFromNacos(String namespace, String serverAddr, String dataId, String group,
long timeOut,String configChangeListener,Map pros){
return HttpRequestUtil.startHttpPoolsFromNacos( namespace, serverAddr, dataId, group,
timeOut, configChangeListener, pros);
}
public static ResourceStartResult startHttpPoolsFromNacosAwaredChange(String namespace, String serverAddr, String dataId, String group, long timeOut,
Map pros){
return HttpRequestUtil.startHttpPoolsFromNacosAwaredChange( namespace, serverAddr, dataId, group, timeOut, pros);
}
public static String httpGetforString(String url) throws HttpProxyRequestException {
return httpGetforString(url, (String) null, (String) null, (Map) null);
}
public static T httpGetforObject(String url,final Class resultType) throws HttpProxyRequestException {
return httpGetforString("default",url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler() {
@Override
public T handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse(url, response, resultType);
}
});
}
public static String httpGetforString(String poolname, String url) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null, (Map) null);
}
public static T httpGetforObject(String poolname, String url,final Class resultType) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler() {
@Override
public T handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url,response, resultType);
}
});
}
public static T httpGetforObjectWithParams(String poolname, String url, Object params,Class resultType) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null,params,(Map)null, new BaseURLResponseHandler() {
@Override
public T handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url,response, resultType);
}
});
}
public static T httpGetforObjectWithParams( String url, Object params,Class resultType) throws HttpProxyRequestException {
return httpGetforString((String)null, url, (String) null, (String) null,params,(Map)null, new BaseURLResponseHandler() {
@Override
public T handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url,response, resultType);
}
});
}
public static T httpGetforObjectWithParamsHeaders(String poolname, String url, Object params,Map headers,Class resultType) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null,params,headers, new BaseURLResponseHandler() {
@Override
public T handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url,response, resultType);
}
});
}
public static T httpGetforObjectWithParamsHeaders( String url, Object params,Map headers,Class resultType) throws HttpProxyRequestException {
return httpGetforString((String)null, url, (String) null, (String) null,params,headers, new BaseURLResponseHandler() {
@Override
public T handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url,response, resultType);
}
});
}
public static T httpGetforObject(String poolname, String url,Map headers,final Class resultType) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null, headers, new BaseURLResponseHandler() {
@Override
public T handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url,response, resultType);
}
});
}
public static List httpGetforList(String url,final Class resultType) throws HttpProxyRequestException {
return httpGetforString("default",url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler>() {
@Override
public List handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleListResponse( url,response, resultType);
}
});
}
public static Map httpGetforMap(String url,final Class keyType,final Class resultType) throws HttpProxyRequestException {
return httpGetforString("default",url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler >() {
@Override
public Map handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleMapResponse( url,response,keyType, resultType);
}
});
}
public static Set httpGetforSet(String url,final Class resultType) throws HttpProxyRequestException {
return httpGetforString("default",url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler>() {
@Override
public Set handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleSetResponse(url, response, resultType);
}
});
}
public static List httpGetforList(String poolName,String url,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler>() {
@Override
public List handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleListResponse( url, response, resultType);
}
});
}
public static List httpGetforList(String poolName,String url,Object params,Map headers,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null,params, headers, new BaseURLResponseHandler>() {
@Override
public List handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleListResponse( url, response, resultType);
}
});
}
public static List httpGetforList(String poolName,String url,Object params,Map headers,final Class resultType,BaseURLResponseHandler> baseURLResponseHandler) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, params,headers,baseURLResponseHandler);
}
public static List httpGetforList(String poolName,String url,Map headers,final Class resultType,BaseURLResponseHandler> baseURLResponseHandler) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, headers, baseURLResponseHandler);
}
public static List httpGetforList(String poolName,String url,Map headers,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, headers, new BaseURLResponseHandler>() {
@Override
public List handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleListResponse( url, response, resultType);
}
});
}
public static D httpGetforTypeObject(String url,final Class containType,final Class resultType) throws HttpProxyRequestException {
return httpGetforTypeObject("default", url, containType, resultType);
}
public static D httpGetforTypeObject(String poolName,String url,final Class containType,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler() {
@Override
public D handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url, response,containType, resultType);
}
});
}
public static D httpGetforTypeObjectWithParams(String url,Object params,final Class containType,final Class resultType) throws HttpProxyRequestException {
return httpGetforTypeObject("default", url, params,containType, resultType);
}
public static D httpGetforTypeObject(String poolName,String url,Object params,final Class containType,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, params,(Map) null, new BaseURLResponseHandler() {
@Override
public D handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url, response,containType, resultType);
}
});
}
public static D httpGetforTypeObjectWithHeader(String url,Object params,Map headers,final Class containType,final Class resultType) throws HttpProxyRequestException {
return httpGetforTypeObject("default", url, params,headers,containType, resultType);
}
public static D httpGetforTypeObject(String poolName,String url,Object params,Map headers,final Class containType,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, params,headers, new BaseURLResponseHandler() {
@Override
public D handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleResponse( url, response,containType, resultType);
}
});
}
public static Map httpGetforMap(String poolName,String url,final Class keyType,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler >() {
@Override
public Map handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleMapResponse( url, response,keyType, resultType);
}
});
}
public static Map httpGetforMap(String poolName,String url,Map headers,final Class keyType,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, headers, new BaseURLResponseHandler >() {
@Override
public Map handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleMapResponse( url, response,keyType, resultType);
}
});
}
public static Set httpGetforSet(String poolName,String url,Map headers,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, headers, new BaseURLResponseHandler>() {
@Override
public Set handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleSetResponse( url, response, resultType);
}
});
}
public static Set httpGetforSet(String poolName,String url,final Class resultType) throws HttpProxyRequestException {
return httpGetforString( poolName,url, (String) null, (String) null, (Map) null, new BaseURLResponseHandler>() {
@Override
public Set handleResponse(final HttpResponse response)
throws IOException {
return ResponseUtil.handleSetResponse( url, response, resultType);
}
});
}
public static T httpGet(String poolname, String url,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null, (Map) null,responseHandler);
}
public static T httpGet(String poolname, String url,Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null, headers,responseHandler);
}
public static T httpGet(String url,Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpGetforString("default", url, (String) null, (String) null, headers,responseHandler);
}
public static String httpGetforString(String url, Map headers) throws HttpProxyRequestException {
return httpGetforString(url, (String) null, (String) null, headers);
}
public static String httpGetforString(String poolname, String url, Map headers) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null, headers);
}
public static String httpGetforStringWithParams(String poolname, String url, Object params) throws HttpProxyRequestException {
// return httpGetforString(poolname, url, (String) null, (String) null, (Map)null);
return httpGet( poolname, url, (String) null, (String) null, params,(Map)null, new StringResponseHandler());
}
public static String httpGetforStringWithParams( String url, Object params) throws HttpProxyRequestException {
// return httpGetforString(poolname, url, (String) null, (String) null, (Map)null);
return httpGet( (String)null, url, (String) null, (String) null, params,(Map)null, new StringResponseHandler());
}
public static String httpGetforStringWithParamsHeaders(String poolname, String url, Object params,Map headers) throws HttpProxyRequestException {
// return httpGetforString(poolname, url, (String) null, (String) null, (Map)null);
return httpGet( poolname, url, (String) null, (String) null, params,headers, new StringResponseHandler());
}
public static String httpGetforStringWithParamsHeaders( String url, Object params,Map headers) throws HttpProxyRequestException {
// return httpGetforString(poolname, url, (String) null, (String) null, (Map)null);
return httpGet( (String)null, url, (String) null, (String) null, params,headers, new StringResponseHandler());
}
public static T httpGetforString(String poolname, String url, Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpGetforString(poolname, url, (String) null, (String) null, headers,responseHandler);
}
public static T httpGetforString( String url, Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpGetforString("default", url, (String) null, (String) null, headers,responseHandler);
}
public static String httpGetforString(String url, String cookie, String userAgent, Map headers) throws HttpProxyRequestException {
return httpGetforString("default", url, cookie, userAgent, headers);
}
public static String httpGetforString(String poolname, String url, String cookie, String userAgent, Map headers) throws HttpProxyRequestException{
return httpGetforString(poolname, url, cookie, userAgent, headers,new StringResponseHandler()) ;
}
/**
* get请求URL
*
* @param url
* @throws HttpProxyRequestException
*/
public static T httpGetforString(String poolname, String url, String cookie, String userAgent, Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpGet( poolname, url, cookie, userAgent, (Map)null, headers, responseHandler);
}
/**
* get请求URL
*
* @param url
* @throws HttpProxyRequestException
*/
public static T httpGetforString(String poolname, String url, String cookie, String userAgent,Object params, Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpGet( poolname, url, cookie, userAgent, params, headers, responseHandler);
}
/**
* get请求URL
*
* @param url
* @throws HttpProxyRequestException
*/
public static T httpGet(String poolname, String url, final String cookie,final String userAgent,final Object params,final Map headers,final ResponseHandler responseHandler) throws HttpProxyRequestException {
//拼接get请求参数
url = HttpParamsHandler.appendParams(url,params);
return _handleRequest( poolname, url ,
responseHandler,new ExecuteRequest(){
@Override
public Object execute(ClientConfiguration config, HttpClient httpClient,String url, int triesCount) throws Exception {
HttpGet httpGet = null;
try {
httpGet = HttpRequestUtil.getHttpGet(config, url, cookie, userAgent, headers);
return httpClient.execute(httpGet, responseHandler);
}
finally {
// 释放连接
if (httpGet != null)
httpGet.releaseConnection();
}
}
} );
}
/**
* head请求URL
*
* @param url
* @throws HttpProxyRequestException
*/
public static T httpHead(String poolname, String url,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpHead( poolname, url, null, null, (Map) null,responseHandler);
}
/**
* get请求URL
*
* @param url
* @throws HttpProxyRequestException
*/
public static T httpHead(String url,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpHead( "default", url, null, null, (Map) null,responseHandler);
}
/**
* head请求URL
*
* @param url
* @throws HttpProxyRequestException
*/
public static T httpHead(String poolname, String url,Object params,Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpHead( poolname, url, null, null,params, (Map) headers,responseHandler);
}
/**
* get请求URL
* ,Object params,Map headers,
* @param url
* @throws HttpProxyRequestException
*/
public static T httpHead(String poolname, String url, String cookie, String userAgent, Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpHead( poolname, url, cookie, userAgent,(Map )null, headers,responseHandler);
}
/**
* get请求URL
* ,Object params,Map headers,
* @param url
* @throws HttpProxyRequestException
*/
public static T httpHead(String poolname, String url, final String cookie, final String userAgent,final Object params, final Map headers,
final ResponseHandler responseHandler) throws HttpProxyRequestException {
// String cookie = getCookie();
// String userAgent = getUserAgent();
return _handleRequest( poolname, url ,
responseHandler,new ExecuteRequest(){
@Override
public Object execute(ClientConfiguration config, HttpClient httpClient,String url, int triesCount) throws Exception {
HttpHead httpHead = null;
try {
httpHead = HttpRequestUtil.getHttpHead(config, url, cookie, userAgent, headers);
if (params != null) {
HttpParams httpParams = HttpParamsHandler.httpParams( params);
// Iterator it = params.entrySet().iterator();
// for (int i = 0; it.hasNext(); i++) {
// Entry entry = it.next();
// httpParams.setParameter(String.valueOf(entry.getKey()), entry.getValue());
// }
if(httpParams != null)
httpHead.setParams(httpParams);
}
return httpClient.execute(httpHead, responseHandler);
}
finally {
// 释放连接
if (httpHead != null)
httpHead.releaseConnection();
}
}
} );
}
/**
* 公用post方法
*
* @param url
* @param params
* @param files
* @throws HttpProxyRequestException
*/
public static String httpPostFileforString(String url, Object params, Map files)
throws HttpProxyRequestException {
return httpPostFileforString("default", url, (String) null, (String) null, params, files);
}
public static String httpPostFileforString(String poolname, String url, Object params, Map files)
throws HttpProxyRequestException {
return httpPostFileforString(poolname, url, (String) null, (String) null, params, files);
}
public static String httpPostforStringWithParams(String url, Object params) throws HttpProxyRequestException {
return httpPostforString((String)null,url, params);
}
public static T httpPostWithParams(String url, Object params,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpPostforString(url, params, (Map) null, responseHandler);
}
public static T httpPostForObject(String url, Object params, final Class resultType) throws HttpProxyRequestException {
return httpPostforString(url, params, (Map) null, new BaseURLResponseHandler() {
@Override
public T handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleResponse(url,response,resultType);
}
});
}
public static List httpPostForListWithParams(String url, Object params, final Class resultType) throws HttpProxyRequestException {
return httpPostforString(url, params, (Map) null, new BaseURLResponseHandler>() {
@Override
public List handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleListResponse(url,response,resultType);
}
});
}
public static Set httpPostForSet(String url, Object params, final Class resultType) throws HttpProxyRequestException {
return httpPostforString(url, params, (Map) null, new BaseURLResponseHandler>() {
@Override
public Set handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleSetResponse(url,response,resultType);
}
});
}
public static Map httpPostForMap(String url, Object params, final Class keyType, final Class resultType) throws HttpProxyRequestException {
return httpPostforString(url, params, (Map) null, new BaseURLResponseHandler>() {
@Override
public Map handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleMapResponse(url,response,keyType,resultType);
}
});
}
public static T httpPostForObject(String poolName,String url, Object params, final Class resultType) throws HttpProxyRequestException {
HttpOption httpOption = new HttpOption();
httpOption.params = params;
return httpPost( poolName, url, httpOption, new BaseURLResponseHandler() {
@Override
public T handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleResponse(url,response,resultType);
}
});
// return httpPostforString( poolName,url, params, (Map) null);
}
public static T httpPostForObject(String poolName,String url, Object params, final Class resultType,DataSerialType dataSerialType) throws HttpProxyRequestException {
HttpOption httpOption = new HttpOption();
httpOption.params = params;
httpOption.dataSerialType = dataSerialType;
return httpPost( poolName, url, httpOption, new BaseURLResponseHandler() {
@Override
public T handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleResponse(url,response,resultType);
}
});
// return httpPostforString( poolName,url, params, (Map) null);
}
public static List httpPostForList(String poolName,String url, Object params, final Class resultType,DataSerialType dataSerialType) throws HttpProxyRequestException {
HttpOption httpOption = new HttpOption();
httpOption.params = params;
httpOption.dataSerialType = dataSerialType;
return httpPost( poolName, url, httpOption, new BaseURLResponseHandler>() {
@Override
public List handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleListResponse(url,response,resultType);
}
});
}
public static Set httpPostForSet(String poolName,String url, Object params, final Class resultType,DataSerialType dataSerialType) throws HttpProxyRequestException {
HttpOption httpOption = new HttpOption();
httpOption.params = params;
httpOption.dataSerialType = dataSerialType;
return httpPost( poolName, url, httpOption, new BaseURLResponseHandler>() {
@Override
public Set handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleSetResponse(url,response,resultType);
}
});
// return httpPostforString( poolName,url, params, (Map) null, new ResponseHandler>() {
// @Override
// public Set handleResponse(HttpResponse response) throws IOException {
// return ResponseUtil.handleSetResponse(response,resultType);
// }
// });
}
public static Map httpPostForMap(String poolName,String url, Object params, final Class keyType, final Class resultType,DataSerialType dataSerialType) throws HttpProxyRequestException {
HttpOption httpOption = new HttpOption();
httpOption.params = params;
httpOption.dataSerialType = dataSerialType;
return httpPost( poolName, url, httpOption,new BaseURLResponseHandler>() {
@Override
public Map handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleMapResponse(url,response,keyType,resultType);
}
});
// return httpPostforString(poolName,url, params, (Map) null, new ResponseHandler>() {
// @Override
// public Map handleResponse(HttpResponse response) throws IOException {
// return ResponseUtil.handleMapResponse(response,keyType,resultType);
// }
// });
}
public static List httpPostForList(String poolName,String url, Object params, final Class resultType) throws HttpProxyRequestException {
return httpPostforString( poolName,url, params, (Map) null, new BaseURLResponseHandler>() {
@Override
public List handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleListResponse(url,response,resultType);
}
});
}
public static List httpPostForList(String poolName,String url, Object params, Map headers, final Class resultType) throws HttpProxyRequestException {
return httpPostforString( poolName,url, params, headers, new BaseURLResponseHandler>() {
@Override
public List handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleListResponse(url,response,resultType);
}
});
}
public static List httpPostForList(String poolName,String url, Object params, Map headers, final Class resultType,BaseURLResponseHandler> baseURLResponseHandler) throws HttpProxyRequestException {
return httpPostforString( poolName,url, params, headers, baseURLResponseHandler);
}
public static List httpPostForList(String poolName,String url , final Class resultType) throws HttpProxyRequestException {
return httpPostforString( poolName,url, (Map) null, (Map) null, new BaseURLResponseHandler>() {
@Override
public List handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleListResponse(url,response,resultType);
}
});
}
public static List httpPostForList(String url , final Class resultType) throws HttpProxyRequestException {
return httpPostforString( (String)null,url, (Map) null, (Map) null, new BaseURLResponseHandler>() {
@Override
public List handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleListResponse(url,response,resultType);
}
});
}
public static Set httpPostForSet(String poolName,String url, Object params, final Class resultType) throws HttpProxyRequestException {
return httpPostforString( poolName,url, params, (Map) null, new BaseURLResponseHandler>() {
@Override
public Set handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleSetResponse(url,response,resultType);
}
});
}
public static Map httpPostForMap(String poolName,String url, Object params, final Class keyType, final Class resultType) throws HttpProxyRequestException {
return httpPostforString(poolName,url, params, (Map) null, new BaseURLResponseHandler>() {
@Override
public Map handleResponse(HttpResponse response) throws IOException {
return ResponseUtil.handleMapResponse(url,response,keyType,resultType);
}
});
}
/**
* 公用post方法
*
* @param url
* @param params
* @param headers
* @throws HttpProxyRequestException
*/
public static String httpPostforStringWithHeaders(String url, Object params, Map headers) throws HttpProxyRequestException {
return httpPostFileforString("default", url, (String) null, (String) null, params, (Map) null, headers);
}
/**
* 公用post方法
*
* @param url
* @param params
* @param headers
* @throws HttpProxyRequestException
*/
public static String httpPostforString(String poolName,String url, Object params, Map headers) throws HttpProxyRequestException {
return httpPostFileforString(poolName, url, (String) null, (String) null, params, (Map) null, headers);
}
/**
* 公用post方法
*
* @param url
* @param params
* @param headers
* @throws HttpProxyRequestException
*/
public static T httpPost(String url, Object params, Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpPost("default", url, (String) null, (String) null, params, (Map) null, headers, responseHandler);
}
/**
* 公用post方法
*
* @param url
* @param params
* @param headers
* @throws HttpProxyRequestException
*/
public static T httpPostforString(String url, Object params, Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpPost("default", url, (String) null, (String) null, params, (Map) null, headers,responseHandler);
}
/**
* 公用post方法
*
* @param url
* @param params
* @param headers
* @throws HttpProxyRequestException
*/
public static T httpPostforString(String poolName,String url, Object params, Map headers,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpPost(poolName, url, (String) null, (String) null, params, (Map) null, headers,responseHandler);
}
public static String httpPostforString(String poolname, String url, Object params) throws HttpProxyRequestException {
HttpOption httpOption = new HttpOption();
httpOption.params = params;
return httpPost( poolname, url, httpOption,new StringResponseHandler());
// return httpPostFileforString(poolname, url, (String) null, (String) null, params, (Map) null);
}
public static String httpPostforString(String poolname, String url, Object params,DataSerialType dataSerialType) throws HttpProxyRequestException {
HttpOption httpOption = new HttpOption();
httpOption.params = params;
httpOption.dataSerialType = dataSerialType;
return httpPost( poolname, url, httpOption,new StringResponseHandler());
// return httpPostFileforString(poolname, url, (String) null, (String) null, params, (Map) null);
}
public static String httpPostforString(String url) throws HttpProxyRequestException {
return httpPostforString("default", url,(Object)null);
}
public static T httpPost(String url,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpPost("default", url, responseHandler);
}
/**
* 公用post方法
*
* @param poolname
* @param url
* @throws HttpProxyRequestException
*/
public static String httpPostforString(String poolname, String url) throws HttpProxyRequestException {
return httpPostFileforString(poolname, url, (String) null, (String) null, (Map) null,
(Map) null);
}
/**
* 公用post方法
*
* @param poolname
* @param url
* @throws HttpProxyRequestException
*/
public static T httpPost(String poolname, String url,ResponseHandler responseHandler) throws HttpProxyRequestException {
return httpPost( poolname, url, (String) null, (String) null, (Map) null,
(Map) null, (Map)null,responseHandler) ;
}
public static String httpPostforStringWithFiles(String url, String cookie, String userAgent,
Map files) throws HttpProxyRequestException {
return httpPostforString("default", url, cookie, userAgent,
files);
}
public static String httpPostforString(String poolname, String url, String cookie, String userAgent,
Map files) throws HttpProxyRequestException {
return httpPostFileforString(poolname, url, cookie, userAgent, null,
files);
}
public static String httpPostforStringWithParams(String url, String cookie, String userAgent, Object params,
Map files) throws HttpProxyRequestException {
return httpPostFileforString("default", url, cookie, userAgent, params,
files);
}
public static String httpPostFileforString(String poolname, String url, String cookie, String userAgent, Object params,
Map files) throws HttpProxyRequestException {
return httpPostFileforString(poolname, url, cookie, userAgent, params,
files, null);
}
public static class HttpOption{
public String cookie;
public String userAgent;
public Object params;//只能是map或者PO对象
public Map files;
public Map headers;
public DataSerialType dataSerialType = DataSerialType.TEXT;
}
/**
* 公用post方法
*
* @param poolname
* @param url
* @param httpOption
* @throws HttpProxyRequestException
*/
public static T httpPost(String poolname, String url, final HttpOption httpOption,final ResponseHandler responseHandler) throws HttpProxyRequestException {
// System.out.println("post_url==> "+url);
// String cookie = getCookie(appContext);
// String userAgent = getUserAgent(appContext);
HttpEntity httpEntity = null;
List paramPair = null;
if (httpOption.files != null) {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
// post表单参数处理
// int length = (httpOption.params == null ? 0 : httpOption.params.size()) + (httpOption.files == null ? 0 : httpOption.files.size());
// int i = 0;
boolean hasdata = HttpParamsHandler.paramsHandle( multipartEntityBuilder,httpOption);
// if (httpOption.params != null) {
// Iterator it = httpOption.params.entrySet().iterator();
// while (it.hasNext()) {
// Entry entry = it.next();
// if(entry.getValue() == null)
// continue;
// if(httpOption.dataSerialType != DataSerialType.JSON || entry.getValue() instanceof String) {
// multipartEntityBuilder.addTextBody(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()), ClientConfiguration.TEXT_PLAIN_UTF_8);
// }
// else{
//
// multipartEntityBuilder.addTextBody(String.valueOf(entry.getKey()), SimpleStringUtil.object2json(entry.getValue()), ClientConfiguration.TEXT_PLAIN_UTF_8);
// }
// hasdata = true;
// }
// }
if (httpOption.files != null) {
Iterator> it = httpOption.files.entrySet().iterator();
while (it.hasNext()) {
Entry entry = it.next();
// parts[i++] = new FilePart(entry.getKey(), entry.getValue());
File f = new File(String.valueOf(entry.getValue()));
if (f.exists()) {
FileBody file = new FileBody(f);
multipartEntityBuilder.addPart(entry.getKey(), file);
hasdata = true;
}
// System.out.println("post_key_file==> "+file);
}
}
if (hasdata)
httpEntity = multipartEntityBuilder.build();
} else if (httpOption.params != null ) {
paramPair = HttpParamsHandler.paramsPaires( httpOption) ;
// paramPair = new ArrayList();
// Iterator