Welcome to Yumao′s Blog.
跑在Web容器中的話
可以在監聽器中直接初始化HC
之後就可以用static方法進行調用
用做entity內容獲取
Code如下:
public class ThreadHttpClient { private static Logger logger = Logger.getLogger(ThreadHttpClient.class); private static ThreadHttpClient threadhttpclient; private DefaultHttpClient httpclient; public static void load(){ threadhttpclient = new ThreadHttpClient(); } public static ThreadHttpClient getInstance() { if(threadhttpclient == null){ load(); } return threadhttpclient; } public void initHttp() { logger.info("init ThreadHttpClient..."); //設置最大連接數 PoolingClientConnectionManager cm = new PoolingClientConnectionManager(); int maxTotal = 128; cm.setMaxTotal(maxTotal); this.httpclient = new DefaultHttpClient(cm); } public void shutHttp(){ if(null != httpclient){ logger.info("關閉HttpClient多線程處理"); httpclient.getConnectionManager().shutdown(); } } public DefaultHttpClient getHttpclient() { return httpclient; } public void setHttpclient(DefaultHttpClient httpclient) { this.httpclient = httpclient; } }
public class ClientHttp { private static Logger logger = Logger.getLogger(ClientHttp.class); /** * 不帶參數的Get方法 * @param httpUrl * 鏈接地址 * @return HTTP響應的字符串 */ public static String doGet(String httpUrl){ DefaultHttpClient http=ThreadHttpClient.getInstance().getHttpclient(); //設置超時時間 setTimeout(http); //設置Get地址 HttpGet get=new HttpGet(httpUrl); String entity = ""; HttpResponse response=null; //開始發送請求獲取數據 try{ response=http.execute(get); if (response.getStatusLine().getStatusCode() == 200) { entity=EntityUtils.toString(response.getEntity()); }else{ logger.error("GET請求返回失敗"); entity = "ErrorCode:0001"; } }catch(org.apache.http.conn.ConnectTimeoutException e1){ logger.error("HttpClient發送請求超時"); entity = "ErrorCode:0011"; }catch(java.net.SocketTimeoutException e2){ logger.error("HttpClient讀取數據超時"); entity = "ErrorCode:0100"; }catch(Exception e){ logger.error("GET請求發送失敗:"+e.toString()); entity = "ErrorCode:0010"; }finally{ //釋放鏈接 get.abort(); } return entity; } /** * 帶參數的Get方法 * @param httpUrl * 鏈接地址 * @param parameters * 參數 x=y&z=a... * @return HTTP響應的字符串/出錯信息 */ public static String doGet(String httpUrl,String parameters){ DefaultHttpClient http=ThreadHttpClient.getInstance().getHttpclient(); //設置超時時間 setTimeout(http); //設置Get地址 httpUrl=httpUrl+"?"+parameters; HttpGet get=new HttpGet(httpUrl); String entity = ""; HttpResponse response=null; //開始發送請求獲取數據 try{ response=http.execute(get); if (response.getStatusLine().getStatusCode() == 200) { entity=EntityUtils.toString(response.getEntity()); }else{ logger.error("GET請求返回失敗"); entity = "ErrorCode:0001"; } }catch(org.apache.http.conn.ConnectTimeoutException e1){ logger.error("HttpClient發送請求超時"); entity = "ErrorCode:0011"; }catch(java.net.SocketTimeoutException e2){ logger.error("HttpClient讀取數據超時"); entity = "ErrorCode:0100"; }catch(Exception e){ logger.error("GET請求發送失敗:"+e.toString()); entity = "ErrorCode:0010"; }finally{ //釋放鏈接 get.abort(); } return entity; } /** * 帶參數的Get方法 * @param httpUrl * 鏈接地址 * @param parameters * 類型:Map * @return HTTP響應的字符串 */ public static String doGet(String httpUrl,Map<String,String> parameters){ DefaultHttpClient http=ThreadHttpClient.getInstance().getHttpclient(); //設置超時時間 setTimeout(http); //設置Get地址 httpUrl=httpUrl+"?"; Iterator it = parameters.entrySet().iterator(); int count=0; while(it.hasNext()){ if(count==0){ count++; }else{ httpUrl=httpUrl+"&"; } Map.Entry m=(Map.Entry)it.next(); httpUrl=httpUrl+m.getKey()+"="+m.getValue(); } logger.info("請求地址組織完畢 : " + httpUrl); HttpGet get=new HttpGet(httpUrl); String entity = ""; HttpResponse response=null; //開始發送請求獲取數據 try{ response=http.execute(get); if (response.getStatusLine().getStatusCode() == 200) { entity=EntityUtils.toString(response.getEntity()); }else{ logger.error("GET請求返回失敗"); entity = "ErrorCode:0001"; } }catch(org.apache.http.conn.ConnectTimeoutException e1){ logger.error("HttpClient發送請求超時"); entity = "ErrorCode:0011"; }catch(java.net.SocketTimeoutException e2){ logger.error("HttpClient讀取數據超時"); entity = "ErrorCode:0100"; }catch(Exception e){ logger.error("GET請求發送失敗:"+e.toString()); entity = "ErrorCode:0010"; }finally{ //釋放鏈接 get.abort(); } return entity; } /** * 帶參數的Post方法 * @param httpUrl * 鏈接地址 * @param parameters * 參數 x=y&z=a... * @param StrCode * 字符編碼 * @return HTTP響應的字符串 */ public static String doPost(String httpUrl,String parameters,String StrCode){ DefaultHttpClient http=ThreadHttpClient.getInstance().getHttpclient(); //設置超時時間 setTimeout(http); //設置Post地址 HttpPost post=new HttpPost(httpUrl); //組織Post內容 List<NameValuePair> params = new ArrayList<NameValuePair>(); String para[]=parameters.split("&"); for(int i=0;i<para.length;i++){ params.add(new BasicNameValuePair(para[i].substring(0,para[i].indexOf("=")),para[i].substring(para[i].indexOf("=")+1))); } //發送請求 String entity = ""; HttpResponse response=null; //開始發送請求獲取數據 try{ post.setEntity(new UrlEncodedFormEntity(params,StrCode)); response=http.execute(post); if (response.getStatusLine().getStatusCode() == 200) { entity=EntityUtils.toString(response.getEntity()); }else{ logger.error("POST請求返回失敗"); entity = "ErrorCode:0001"; } }catch(org.apache.http.conn.ConnectTimeoutException e1){ logger.error("HttpClient發送請求超時"); entity = "ErrorCode:0011"; }catch(java.net.SocketTimeoutException e2){ logger.error("HttpClient讀取數據超時"); entity = "ErrorCode:0100"; }catch(Exception e){ logger.error("POST請求發送失敗:"+e.toString()); entity = "ErrorCode:0010"; }finally{ //釋放鏈接 post.abort(); } return entity; } /** * 帶參數的Post方法 * @param httpUrl 鏈接地址 * @param parameters 參數Map * @param StrCode 字符編碼 * @return HTTP響應的字符串 */ public static String doPost(String httpUrl,Map<String,String> parameters,String StrCode){ DefaultHttpClient http=ThreadHttpClient.getInstance().getHttpclient(); //設置超時時間 setTimeout(http); //設置Post地址 HttpPost post=new HttpPost(httpUrl); //組織Post內容 List<NameValuePair> params = new ArrayList<NameValuePair>(); Iterator it = parameters.entrySet().iterator(); while(it.hasNext()){ Map.Entry m=(Map.Entry)it.next(); params.add(new BasicNameValuePair(m.getKey()+"",m.getValue()+"")); } //發送請求 String entity = ""; HttpResponse response=null; //開始發送請求獲取數據 try{ post.setEntity(new UrlEncodedFormEntity(params,StrCode)); response=http.execute(post); if (response.getStatusLine().getStatusCode() == 200) { entity=EntityUtils.toString(response.getEntity()); }else{ logger.error("POST請求返回失敗"); entity = "ErrorCode:0001"; } }catch(org.apache.http.conn.ConnectTimeoutException e1){ logger.error("HttpClient發送請求超時"); entity = "ErrorCode:0011"; }catch(java.net.SocketTimeoutException e2){ logger.error("HttpClient讀取數據超時"); entity = "ErrorCode:0100"; }catch(Exception e){ logger.error("POST請求發送失敗:"+e.toString()); entity = "ErrorCode:0010"; }finally{ //釋放鏈接 post.abort(); } return entity; } public static void setTimeout(DefaultHttpClient http){ //設置打開鏈接超時時間 http.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000); //設置讀取數據超時超時時間 http.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); } }
辣么高端,不懂啊。