httpclient

 

package test;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.apache.commons.codec.digest.DigestUtils;
public class HttpClientApache {

    private HttpsURLConnection getHttpsConn() {
        try {
            URL url = new URL("请求地址");
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            SSLContext sc;
            sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[] { new MyTrustManager() },new SecureRandom());
            conn.setSSLSocketFactory(sc.getSocketFactory());
            return conn;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * write
     */
    public void write() {

        HttpsURLConnection conn = getHttpsConn();
        DataOutputStream out = null;
        try {
            conn.setConnectTimeout(120 * 1000); 
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            conn.connect();
            String filenameToSend = "报文.xml";
            File f = new File(filenameToSend);
            byte data[] = new byte[(int) f.length()];
            FileInputStream fis = new FileInputStream(filenameToSend);
            fis.read(data);
            fis.close();
            String xmlStr = new String(data);
            // 获取xml报文data节点下的所有值
            String dataStr = getNodeValue(xmlStr, "bizdata", "data");
            System.out.println("dataStr:" + dataStr);
//采用sha1加密 String sign
= DigestUtils.sha1Hex(dataStr+秘钥); System.out.println("sign:" + sign); // 设置加签sign到xml报文中 String returnXml = getSignXMLString(xmlStr, sign); System.out.println("returnXml:" + returnXml); data = returnXml.getBytes(); out = new DataOutputStream(conn.getOutputStream()); out.write(org.apache.commons.codec.binary.Base64.encodeBase64(data)); out.flush(); out.close(); System.out.println(conn.getResponseCode() + ">>"+ conn.getResponseMessage()); byte[] bytes = StreamUtil.toByteArray(conn.getInputStream()); if (bytes == null || bytes.length == 0) { System.out.println("time out"); } else { // 返回 String ret = new String( org.apache.commons.codec.binary.Base64.decodeBase64(bytes),"GBK"); String code = XmlUtil.getNodeValue(ret, "code"); System.out.println("------------------:" + code); System.out.println(ret); } conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { HttpClientApache a = new HttpClientApache(); for (int i = 0; i < 1; i++) { a.write(); } System.exit(0); } public static String getNodeValue(String xmlString, String... names) { if ((names == null) || (names.length == 0)) { return null; } String nodeValue = xmlString; for (String name : names) { nodeValue = getNodeValue(nodeValue, name); if (nodeValue == null) { return null; } } return nodeValue; } public static String getNodeValue(String xmlString, String name) { if (xmlString == null) { return null; } int startPos = xmlString.indexOf("<" + name + ">"); if (startPos == -1) { return null; } int endPos = xmlString.lastIndexOf("</" + name + ">"); if (endPos == -1) { return null; } return xmlString.substring(startPos + ("</" + name + ">").length() - 1,endPos); } public static String getSignXMLString(String xmlString,String sign) { String StrVal = ""; if (xmlString != null && !"".equals(xmlString)) { boolean isExistStr = xmlString.contains("<sign>"); int a = xmlString.lastIndexOf("<value>"); int b = xmlString.lastIndexOf("</value>"); String requestXML = ""; String requestXMLStr=""; requestXML = xmlString.substring(0, b); requestXMLStr = xmlString.substring(b, xmlString.length()); if (isExistStr) { StrVal = requestXML+sign+requestXMLStr; } else { StrVal = requestXML; } } return StrVal.trim(); } }

 

package test;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;

public class MyTrustManager implements X509TrustManager{

    @Override
    public void checkClientTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
        
    }

    @Override
    public void checkServerTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[]{};
    }

}

 

posted @ 2016-07-12 10:11  神玄晓  阅读(217)  评论(0编辑  收藏  举报