httpClient發送https 請求

來源:互聯網
上載者:User

標籤: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 請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.