1. 程式人生 > >httpclient模擬瀏覽器呼叫服務介面

httpclient模擬瀏覽器呼叫服務介面

用httpclient呼叫服務介面獲取資料返回403,並標記為疑似黑客攻擊。返回錯誤結果如果圖:

這裡寫圖片描述
這裡寫圖片描述
後發現在httpclient需要把User-Agent設定為瀏覽器方式:
String userAgent = “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36”;
httpPost.setHeader(“User-Agent”, userAgent);
完整程式碼如下:

/**
 * Http客戶端管理器
 * @author
Tiger * */
public class HttpConnectionManager { private static int SOCKET_TIMEOUT = 30000; //socket超時時間 private static int CONNECTION_TIMEOUT = 30000; //連線超時 private static SSLConnectionSocketFactory socketFactory = null; //私密連結工廠 private static RequestConfig defaultRequestconfig = null; private
static RequestConfig requestConfig = null; private static Registry<ConnectionSocketFactory> socketFactoryRegistry = null; private static TrustManager manager = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } }; static{ //SSL套位元組 try { SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[]{manager}, null); socketFactory = new SSLConnectionSocketFactory(context,NoopHostnameVerifier.INSTANCE); } catch (NoSuchAlgorithmException | KeyManagementException e) { e.printStackTrace(); } //加密連結工廠建立 socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http",PlainConnectionSocketFactory.INSTANCE).register("https",socketFactory).build(); //請求配置 defaultRequestconfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).setExpectContinueEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM,AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setSocketTimeout(SOCKET_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).build(); requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).build(); } /** * 執行POST請求 * * @param url * @param params * @return * @throws IOException */ public static HttpResult post(String url,JSONObject params) throws Exception { // 建立POST 請求 HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); if(params.containsKey("sign")){ httpPost.addHeader("sign", params.getString("sign")); } // 將請求實體設定到httpPost物件中 httpPost.setEntity(getFormEntity(params)); return getHttpResult(url,httpPost); } /** * 執行POST請求 * * @param url * @param params * @return * @throws IOException */ public static HttpResult postForJSON(String url,JSONObject params) throws Exception { // 建立POST 請求 HttpPost httpPost = new HttpPost(url); String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"; httpPost.setHeader("User-Agent", userAgent); httpPost.setConfig(requestConfig); //放入JSON資料 StringEntity entity = new StringEntity(params.toJSONString()); entity.setContentType("application/json"); httpPost.setEntity(entity); return getHttpResult(url,httpPost); } /** * 執行GET請求 * * @param url * @param params * @return * @throws IOException */ public static HttpResult get(String url,JSONObject params) throws Exception { // 建立POST 請求 url += formatGETParam(params); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); return getHttpResult(url,httpGet); } /** * 執行PUT請求 * * @param url * @param params * @return * @throws IOException */ public static HttpResult putForJSON(String url,JSONObject params) throws Exception { // 建立POST 請求 HttpPut httpPut = new HttpPut(url); httpPut.setConfig(requestConfig); //放入JSON資料 StringEntity entity = new StringEntity(params.toJSONString()); entity.setContentType("application/json"); httpPut.setEntity(entity); return getHttpResult(url,httpPut); } /** * 獲取HttpClient * @param isSSL 是否為https * @return */ public static CloseableHttpClient getHttpClient(boolean isSSL){ // 建立http POST請求 CloseableHttpClient httpClient = null; if(isSSL){ //連結管理器建立 PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(defaultRequestconfig).build(); }else{ httpClient = HttpClients.createDefault(); } return httpClient; } /** * 獲取UrlEncodedFormEntity * @param params * @return * @throws Exception */ public static UrlEncodedFormEntity getFormEntity(JSONObject params) throws Exception { // 設定post引數 List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); Set<String> keys = params.keySet(); Iterator<String> it = keys.iterator(); while(it.hasNext()){ String fieldName = it.next(); String value = params.getString(fieldName); if(value != null) { parameters.add(new BasicNameValuePair(fieldName, value)); } } // 構造一個form表單式的實體 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8"); return formEntity; } /** * 獲取UrlEncodedFormEntity * @param params * @return * @throws Exception */ public static String formatGETParam(JSONObject params) throws Exception { // 設定post引數 String urlParam = "?"; Set<String> keys = params.keySet(); Iterator<String> it = keys.iterator(); while(it.hasNext()){ String fieldName = it.next(); String value = params.getString(fieldName); if(value != null) { urlParam += fieldName+"="+value+"&"; } } urlParam = urlParam.equals("?") ? "" : urlParam.substring(0,urlParam.lastIndexOf("&")); return urlParam; } /** * 獲取HttpResult * @param url * @param method * @return * @throws Exception */ public static HttpResult getHttpResult(String url,HttpUriRequest method) throws Exception{ // 處理返回 HttpResult httpResult; CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try { httpClient = getHttpClient(url.contains("https")); // 建立請求客戶端 System.out.println(method); response = httpClient.execute(method); // 執行請求 httpResult = new HttpResult(response.getStatusLine().getStatusCode(),EntityUtils.toString(response.getEntity(),"UTF-8")); } catch (Exception e){ e.printStackTrace(); httpResult = new HttpResult(500,e.getMessage()); } finally { if (response != null) { response.close(); } if(httpClient != null){ httpClient.close(); } } System.out.println(httpResult); return httpResult; } }