package jetsennet.ia.business; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.methods.DeleteMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.httpclient.params.HttpMethodParams; /************************************************************************ 日 期: 2013-10-14 做 者: 周波 版 本: smg10 描 述: 智能分析http訪問代理 歷 史: ************************************************************************/ public class IAHttpProxy { private final static String USERNAME = "jetsenadmin";//TRS用戶名 private final static String PASSWORD = "jetsen@admin";//TRS密碼 private final static int timeOut = 30000; //毫秒 private final static HttpClient httpClient = new HttpClient(); /** * 發送GET請求 * @param url * @return */ public static String doGetMethod(String url, List<NameValuePair> pList)throws Exception { String result = ""; GetMethod method = new GetMethod(url); method.addRequestHeader("Content-Type", "text/html");//設定請求 HttpMethodParams httpParams = new HttpClientParams(); httpParams.setSoTimeout(timeOut); method.setParams(httpParams); List<NameValuePair> paramsList = new ArrayList<NameValuePair>(); paramsList.add(new NameValuePair("username",USERNAME)); paramsList.add(new NameValuePair("password",PASSWORD)); if(pList != null) { paramsList.addAll(pList); } NameValuePair[] params = new NameValuePair[paramsList.size()]; paramsList.toArray(params); try { method.setQueryString(params); int status = httpClient.executeMethod(method); result = method.getResponseBodyAsString(); } catch (URIException e) { // TODO: handle exception throw new Exception("[執行HTTP Get請求時,發生異常,可能超時。]"); } catch (IOException e) { throw new Exception("[執行HTTP Get請求時,發生異常,可能超時。]"); } finally { method.releaseConnection(); } method.releaseConnection(); return result; } /** * 發送POST請求 * @param url * @return */ public static String doPostMethod(String url, List<NameValuePair> pList)throws Exception { String result = ""; PostMethod method = new PostMethod(url); method.addRequestHeader("Content-Type", "text/html");//設定請求 HttpMethodParams httpParams = new HttpClientParams(); httpParams.setSoTimeout(timeOut); method.setParams(httpParams); List<NameValuePair> paramsList = new ArrayList<NameValuePair>(); paramsList.add(new NameValuePair("username",USERNAME)); paramsList.add(new NameValuePair("password",PASSWORD)); if(pList != null) { paramsList.addAll(pList); } NameValuePair[] params = new NameValuePair[paramsList.size()]; paramsList.toArray(params); try { method.setQueryString(params); int status = httpClient.executeMethod(method); System.out.println("請求結果狀態:"+status); result = method.getResponseBodyAsString(); } catch (URIException e) { // TODO: handle exception throw new Exception("[執行HTTP POST請求時,發生異常,可能超時。]"); } catch (IOException e) { throw new Exception("[執行HTTP POST請求時,發生異常,可能超時。]"); } finally { method.releaseConnection(); } return result; } /** * 發送PUT請求 * @param url * @return */ public static String doPutMethod(String url, List<NameValuePair> pList)throws Exception { String result = ""; PutMethod method = new PutMethod(url); method.addRequestHeader("Content-Type", "text/html");//設定請求 HttpMethodParams httpParams = new HttpClientParams(); httpParams.setSoTimeout(timeOut); method.setParams(httpParams); List<NameValuePair> paramsList = new ArrayList<NameValuePair>(); paramsList.add(new NameValuePair("username",USERNAME)); paramsList.add(new NameValuePair("password",PASSWORD)); if(pList != null) { paramsList.addAll(pList); } NameValuePair[] params = new NameValuePair[paramsList.size()]; paramsList.toArray(params); try { method.setQueryString(params); int status = httpClient.executeMethod(method); result = method.getResponseBodyAsString(); } catch (URIException e) { // TODO: handle exception throw new Exception("[執行HTTP Put請求時,發生異常,可能超時。]"); } catch (IOException e) { throw new Exception("[執行HTTP Put請求時,發生異常,可能超時。]"); } finally { method.releaseConnection(); } return result; } /** * 發送Delete請求 * @param url * @return */ public static String doDeleteMethod(String url, List<NameValuePair> pList)throws Exception { String result = ""; DeleteMethod method = new DeleteMethod(url); method.addRequestHeader("Content-Type", "text/html");//設定請求 HttpMethodParams httpParams = new HttpClientParams(); httpParams.setSoTimeout(timeOut); method.setParams(httpParams); List<NameValuePair> paramsList = new ArrayList<NameValuePair>(); paramsList.add(new NameValuePair("username",USERNAME)); paramsList.add(new NameValuePair("password",PASSWORD)); if(pList != null) { paramsList.addAll(pList); } NameValuePair[] params = new NameValuePair[paramsList.size()]; paramsList.toArray(params); try { method.setQueryString(params); int status = httpClient.executeMethod(method); result = method.getResponseBodyAsString(); } catch (URIException e) { // TODO: handle exception throw new Exception("[執行HTTP delete請求時,發生異常,可能超時。]"); } catch (IOException e) { throw new Exception("[執行HTTP delete請求時,發生異常,可能超時。]"); } finally { method.releaseConnection(); } return result; } }