java http, https

httpclient

 

EntityEnclosingMethod httpMethod = new PostMethod(url);
        
// 此处运用连接池技术。
MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
       
// 设定参数:与每个主机的最大连接数
manager.getParams().setDefaultMaxConnectionsPerHost(10);
       
// 设定参数:客户端的总连接数
manager.getParams().setMaxTotalConnections(20);
       
// 使用连接池技术创建HttpClient对象
HttpClient httpClient = new HttpClient(manager);
       
// 设置超时时间
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);

 

//设置请求头

httpMethod.setRequestHeader(type, value);

 

// 发送含xml消息体

RequestEntity entity = new StringRequestEntity(xml, "text/xml", "UTF-8");

 

httpMethod.setRequestEntity(entity);

 

int resultCode = httpClient.executeMethod(httpMethod);

 

//获取响应消息头

Header[] headers = httpMethod.getResponseHeaders();

 

//返回响应报文

String xml = httpMethod.getResponseBodyAsString();

 

 

https

 

// 重写X509TrustManager类的三个方法,信任服务器证书
    private X509TrustManager xtm = new X509TrustManager()
    {
        public void checkClientTrusted(X509Certificate[] chain, String authType)
        {
        }
       
        public void checkServerTrusted(X509Certificate[] chain, String authType)
        {
        }
       
        public X509Certificate[] getAcceptedIssuers()
        {
            return null;
        }
    };
   
    // 信任主机
    private HostnameVerifier hnv = new HostnameVerifier()
    {
        public boolean verify(String hostname, SSLSession session)
        {
            return true;
        }
    };

 

 //构造SSLContext

      /**
         * 安全套接字的实现类
         */
        SSLContext sslContext = null;
        try
        {
            // sslContext = SSLContext.getInstance("TLS");
            sslContext = SSLContext.getInstance("SSL");
            X509TrustManager[] xtmArray = new X509TrustManager[] {xtm};
            sslContext.init(null, xtmArray, new java.security.SecureRandom());
        }
        catch (GeneralSecurityException gse)
        {
            System.out.println("Https:DataSender:General Security Exception .");
            gse.getStackTrace();
        }
       
        if (sslContext != null)
        {
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        }
       
        HttpsURLConnection.setDefaultHostnameVerifier(hnv);

 

//通过Https发送消息

 

// System.setProperty("javax.net.debug","true");
        URL remoteHostURL;
        HttpsURLConnection httpsUrlConnection = null;
       
        // 建立连接
        try
        {
            remoteHostURL = new URL(ct.getRemoteHostURI());
            System.out.println(ct.getRemoteHostURI());
            httpsUrlConnection = (HttpsURLConnection)remoteHostURL.openConnection();
        }
        catch (Exception _ex)
        {
            System.out.println("Unable to openConnection on URL.");
            _ex.printStackTrace();
        }
       
        byte[] Msg = ct.getMessage();
       
        HttpsURLConnection.setFollowRedirects(true);
        httpsUrlConnection.setInstanceFollowRedirects(true);
       
        httpsUrlConnection.setDoOutput(true);
        httpsUrlConnection.setDoInput(true);
       
        httpsUrlConnection.setRequestMethod("POST");
       
        httpsUrlConnection.setRequestProperty("Content-length", Integer.toString(Msg.length));
        httpsUrlConnection.setRequestProperty("Content-type", "application/x-xxxxxxxx; version=1.0");
        httpsUrlConnection.connect();

      

//发送消息体
        OutputStream outStream = null;
        try
        {
            outStream = httpsUrlConnection.getOutputStream();
            outStream.write(Msg);
            outStream.flush();
        }
        finally
        {
            if (outStream != null)
            {
                outStream.close();
            }
        }
       

//接收响应报文
        int responseCode = httpsUrlConnection.getResponseCode();
       
        InputStream is = httpsUrlConnection.getInputStream();
       
        byte[] b = new byte[is.available()];
       
        is.read(b);
       


        System.out.println(new String(b));

 

 

你可能感兴趣的:(java,.net,xml,Security)