httpclent4.x https 绕过证书发xml。测试通过

/**
 * 
 */
package org.ibmp.gmp.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
 * @author HuangXuebin
 * @date 2012-12-10
 * 云南新接口
 * @Version V 1.0
 */
public class HttpsUtil {
private static DefaultHttpClient client;
 /** 
     * 访问https的网站 
     * @param httpclient 
     */  
    private static void enableSSL(DefaultHttpClient httpclient){  
        //调用ssl  
         try {  
                SSLContext sslcontext = SSLContext.getInstance("TLS");  
                sslcontext.init(null, new TrustManager[] { truseAllManager }, null);  
                SSLSocketFactory sf = new SSLSocketFactory(sslcontext);  
                sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
                Scheme https = new Scheme("https", sf, 443);  
                httpclient.getConnectionManager().getSchemeRegistry().register(https);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
    }  
    /** 
     * 重写验证方法,取消检测ssl 
     */  
    private static TrustManager truseAllManager = new X509TrustManager(){  
  
        public void checkClientTrusted(  
                java.security.cert.X509Certificate[] arg0, String arg1)  
                throws CertificateException {  
            // TODO Auto-generated method stub  
              
        }  
  
        public void checkServerTrusted(  
                java.security.cert.X509Certificate[] arg0, String arg1)  
                throws CertificateException {  
            // TODO Auto-generated method stub  
              
        }  
  
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
            // TODO Auto-generated method stub  
            return null;  
        }  
          
    }; 

     /**
     * Send a XML-Formed string to HTTP Server by post method
     * 
     * @param url
     *            the request URL string
     * @param xmlData
     *            XML-Formed string ,will not check whether this string is
     *            XML-Formed or not
     * @return the HTTP response status code ,like 200 represents OK,404 not
     *         found
     * @throws IOException
     * @throws ClientProtocolException
     */
    public static String post(String url, String xmlData)
    throws ClientProtocolException, IOException {
    	String body = null;
    	if (client == null) {
            // Create HttpClient Object
            client = new DefaultHttpClient();
            enableSSL(client);
            }
      //设置客户端编码
        client.getParams().setParameter("http.protocol.content-charset",HTTP.UTF_8);
        client.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);
        client.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);
        client.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET,HTTP.UTF_8);
     // Post请求
        HttpPost post = new HttpPost(url);
      //设置post编码
        post.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
        post.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);
        post.getParams().setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);
        post.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET, HTTP.UTF_8);


        // Construct a string entity 设置参数
        List<NameValuePair> params = new ArrayList<NameValuePair>();
		 params.add(new BasicNameValuePair("xmldata", xmlData));
		 post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 

        // Execute request and get the response
        HttpResponse response = client.execute(post);
        HttpEntity entityRep = response.getEntity(); 
        if (entityRep != null) {     
            body = EntityUtils.toString(entityRep);
           System.out.println("云南返回报文:"+body);
            // Do not need the rest    
            post.abort();    
        }  
        // Response Header - StatusLine - status code
        // statusCode = response.getStatusLine().getStatusCode();
        return body;
        }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值