httpClient处理
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpConnectionManager; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 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.params.HttpMethodParams; import org.apache.commons.httpclient.util.IdleConnectionTimeoutThread; import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * httpClient处理 */ public class HttpProtocolHandler { /** 连接超时时间,缺省为8秒钟 */ private int defaultConnectionTimeout = 8000; /** 回应超时时间,缺省为30秒钟 */ private int defaultSoTimeout = 30000; /** 闲置连接超时时间,缺省为60秒钟 */ private int defaultIdleConnTimeout = 60000; /**定义每台主机允许的最大连接数,默认为2*/ private int defaultMaxConnPerHost = 80; /**httpConnectionManager管理的最大连接数,默认为20*/ private int defaultMaxTotalConn = 100; /** 默认等待HttpConnectionManager返回连接超时(只有在达到最大连接数时起作用)*/ private static final long defaultHttpConnectionManagerTimeout = 3 * 1000; /** HTTP连接管理器,该连接管理器必须是线程安全的. */ private HttpConnectionManager connectionManager; private static HttpProtocolHandler httpProtocolHandler = new HttpProtocolHandler(); /** 工厂方法 */ public static HttpProtocolHandler getInstance() { return httpProtocolHandler; } /**执行标志*/ private static boolean execFlag = true; /**正在执行文件标志map*/ private static Map<String, Integer> writingFlagMap = new HashMap<String, Integer>(); /**恢复次数map*/ private static Map<String, Integer> reConnectMap = new HashMap<String, Integer>(); public boolean isExecFlag() { return execFlag; } public void setExecFlag(boolean execFlag) { HttpProtocolHandler.execFlag = execFlag; } public static Map<String, Integer> getWritingFlagMap() { return writingFlagMap; } public void setWritingFlagMap(Map<String, Integer> writingFlagMap) { HttpProtocolHandler.writingFlagMap = writingFlagMap; } /** * 私有的构造方法 */ private HttpProtocolHandler() { // 创建一个线程安全的HTTP连接池 connectionManager = new MultiThreadedHttpConnectionManager(); connectionManager.getParams().setDefaultMaxConnectionsPerHost(defaultMaxConnPerHost); connectionManager.getParams().setMaxTotalConnections(defaultMaxTotalConn); connectionManager.getParams().setConnectionTimeout(defaultConnectionTimeout); connectionManager.getParams().setSoTimeout(defaultSoTimeout); connectionManager.getParams().setIntParameter("http.socket.timeout", 30000); IdleConnectionTimeoutThread ict = new IdleConnectionTimeoutThread(); ict.addConnectionManager(connectionManager); ict.setConnectionTimeout(defaultIdleConnTimeout); ict.start(); } /** * 直接读取文件信息(对于文件不大的) * @exception TODO */ public String executeGetString(String requestUrl){ HttpClient httpclient = new HttpClient(connectionManager); httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); HttpMethod method = new GetMethod("http://"+requestUrl); // method.setRequestHeader("Cookie","JSESSIONID="+sessionID); method.getParams().setContentCharset("UTF-8"); String resultStr = ""; try { httpclient.executeMethod(method); resultStr = method.getResponseBodyAsString(); if(method.getStatusCode()!=200){ return null; } } catch (HttpException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; }finally{ method.releaseConnection(); } return resultStr; } /** * 请求文件 * @Description: * @param requestUrl 请求路劲 * @param savePath 保存路径 * @exception TODO */ public boolean executeGetFile(String requestUrl,String savePath){ HttpClient httpclient = new HttpClient(connectionManager); httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); FileOutputStream out = null; // System.out.println("http://"+requestUrl); HttpMethod method = new GetMethod("http://"+requestUrl); InputStream in = null; int len = -1; byte[] b = new byte[1024]; try { httpclient.executeMethod(method); in = method.getResponseBodyAsStream(); if(method.getStatusCode()!=200){ System.out.println("信息:http://"+requestUrl+"访问失败,刷新信息失败。"); return false; } out = new FileOutputStream(new File(savePath)); while((len=in.read(b))!=-1){ out.write(b, 0, len); } out.flush(); } catch (HttpException e) { e.printStackTrace();return false; } catch (IOException e) { e.printStackTrace();return false; }finally{ try { if(out!=null){out.close();} } catch (IOException e) { e.printStackTrace(); } method.releaseConnection(); } return true; } /** * POST带参数请求 * @exception TODO */ public void executePostReq(String requestUrl,Map<String, String> params){ HttpClient httpclient = new HttpClient(connectionManager); httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); // PostMethod pmethod = new PostMethod("http://"+requestUrl); PostMethod pmethod = new PostMethod(requestUrl); NameValuePair[] nvps = new NameValuePair[params.size()]; Set<String> keys1 = params.keySet(); int i = 0; for(String name1:keys1){ NameValuePair p = new NameValuePair(name1,params.get(name1)); nvps[i] = p; i++; } pmethod.setRequestBody(nvps); pmethod.getParams().setContentCharset("UTF-8"); try { httpclient.executeMethod(pmethod); if(pmethod.getStatusCode()!=200){ return; } String resultStr = pmethod.getResponseBodyAsString(); System.out.println(resultStr); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ pmethod.releaseConnection(); } } /** * 直接读取文件信息(对于文件不大的) * @exception TODO */ public String executeWSGetString(String requestUrl){ HttpClient httpclient = new HttpClient(connectionManager); httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); HttpMethod method = new GetMethod("http://"+requestUrl); method.setRequestHeader("Sec-WebSocket-Key", " esAjYnZeGHsnmqBc/BtOfA=="); method.setRequestHeader("Upgrade", "websocket"); method.getParams().setContentCharset("UTF-8"); String resultStr = ""; try { httpclient.executeMethod(method); resultStr = method.getResponseBodyAsString(); if(method.getStatusCode()!=200){ return null; } } catch (HttpException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; }finally{ method.releaseConnection(); } return resultStr; } /** * POST带参数请求 * @exception TODO */ public String executePostReq1(String requestUrl,String paramsStr){ HttpClient httpclient = new HttpClient(connectionManager); httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); PostMethod pmethod = new PostMethod(requestUrl); pmethod.setRequestBody(paramsStr); pmethod.getParams().setContentCharset("UTF-8"); pmethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); pmethod.addRequestHeader("Content-Type","text/html;charset=utf-8"); try { httpclient.executeMethod(pmethod); if(pmethod.getStatusCode()!=200){ return null; } String resultStr = pmethod.getResponseBodyAsString(); return resultStr; } catch (HttpException e) { e.printStackTrace();return null; } catch (IOException e) { e.printStackTrace();return null; }finally{ pmethod.releaseConnection(); } } public static void main(String[] args) { String tokenString = "6c0d584a77e745979a0563ad33516d0f"; // Map<String, String> params = new HashMap<String, String>(); // params.put("tokenKey", tokenString); // String string="http://ip:port/xxx/player"; // httpProtocolHandler.executePostReq(string, params); String string="http://ip:port/xxx/player"+"?"+"tokenString="+tokenString;
System.out.println(httpProtocolHandler.executeGetString(string));
} }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。