標籤:httpcliens https
做項目要用到httpClient發送https到伺服器,要求URL中帶參數,並且發送xml格式的報文,第一次做,在這裡記錄一下,以便以後查詢的協助別人:本文在認證配置正確的前提下執行
用戶端代碼;
public static void httpsRequest() throws Exception { System.out.println("this is https"); KeyStore trustStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(STORE); System.out.println(instream); try { trustStore.load(instream, PASSWD.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("sign", sign("timestamp"+timestamp+"v"+V))); nvps.add(new BasicNameValuePair("timestamp", "timestamp")); nvps.add(new BasicNameValuePair("v", "1.0")); HttpPost httpPost = new HttpPost(URL+"?"+ URLEncodedUtils.format(nvps, "UTF-8")); StringEntity sendbody = new StringEntity(xmlData+getParams(),"UTF-8"); httpPost.setEntity(sendbody); httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); System.out.println("executing request " + httpPost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
用戶端,運行結果
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/58/08/wKioL1SoqzTjgGaNAAFMIKkANaM710.jpg" title="QQ圖片20150104104830.png" alt="wKioL1SoqzTjgGaNAAFMIKkANaM710.jpg" />
伺服器代碼,認證配置在tomcat的server.xml中;
public String infoQuery(){System.out.println("+++++++++++++++++++++++++++++++");try {HttpServletRequest request = ServletActionContext.getRequest();String version = request.getParameter("v");String timestamp = request.getParameter("timestamp");String sign = request.getParameter("sign");System.out.println(version);System.out.println(timestamp);System.out.println(sign);byte[] buffer = new byte[1024];int length = request.getInputStream().read(buffer);String encode = request.getCharacterEncoding();System.out.println(length + " <<<>>> " + encode );byte[] data = new byte[length];System.arraycopy(buffer, 0, data, 0, length);String content = new String(data, encode);System.out.println(content);} catch (IOException e) {logger.error("",e);}System.out.println("ssssssddddddsssssssss");return SUCCESS;}
伺服器運行結果
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/58/08/wKioL1Soq4ui1mW7AAI8MIcG-sI669.jpg" title="QQ圖片20150104105116.jpg" alt="wKioL1Soq4ui1mW7AAI8MIcG-sI669.jpg" />
httpClient發送https 請求