Java请求https方法

来源:互联网 发布:广电网络智慧社区 编辑:程序博客网 时间:2024/06/16 09:50

参考百度链接:https://zhidao.baidu.com/question/1694899703762628988.html

时间:2017-08-30


SSLClient类,继承至HttpClient

import org.apache.http.HttpEntity;  import org.apache.http.HttpResponse;  import org.apache.http.client.HttpClient;  import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.message.BasicHeader;import org.apache.http.util.EntityUtils;import org.json.JSONObject;public class SSLClient  extends DefaultHttpClient{     public SSLClient() throws Exception     {              super();              SSLContext ctx = SSLContext.getInstance("TLS");              X509TrustManager tm = new X509TrustManager()             {                      public void checkClientTrusted(X509Certificate[] chain,String authType) throws CertificateException                     {                    }                      public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException                     {                    }                      public X509Certificate[] getAcceptedIssuers()                     {                          return null;                      }              };              ctx.init(null, new TrustManager[]{tm}, null);              SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);              ClientConnectionManager ccm = this.getConnectionManager();              SchemeRegistry sr = ccm.getSchemeRegistry();              sr.register(new Scheme("https", 443, ssf));          }  }

HttpClient发送post请求的类

import java.security.cert.CertificateException;  import java.security.cert.X509Certificate;  import javax.net.ssl.SSLContext;  import javax.net.ssl.TrustManager;  import javax.net.ssl.X509TrustManager;  import org.apache.http.conn.ClientConnectionManager;  import org.apache.http.conn.scheme.Scheme;  import org.apache.http.conn.scheme.SchemeRegistry;  import org.apache.http.conn.ssl.SSLSocketFactory;  import org.apache.http.impl.client.DefaultHttpClient;public class HttpClientUtil {    public String doPost(String url,JSONObject json,String charset)    {          /*         * 利用HttpClient进行post请求的工具类         */        HttpClient httpClient = null;          HttpPost httpPost = null;          String result = null;          try        {              httpClient = new SSLClient();            httpPost = new HttpPost(url);            StringEntity se = new StringEntity(json.toString(),"UTF-8");            se.setContentType("application/json");            se.setContentEncoding(new BasicHeader("text/json", "application/json"));            httpPost.setEntity(se);            HttpResponse response = httpClient.execute(httpPost);              if(response != null)            {                  HttpEntity resEntity = response.getEntity();                  if(resEntity != null)                {                      result = EntityUtils.toString(resEntity,charset);                 }              }          }catch(Exception ex)        {              ex.printStackTrace();          }          return result;      }  }

测试类

import org.json.JSONArray;import org.json.JSONObject;public class Test extends Thread{    public static void main ( String args[] )    {        //test();     }    //维修企业账户注册    public static  void  test()    {        String method = "https://zjtest.qichedangan.cn/restservices/lcipprodatarest/lcipproaccountcompany/query";        try        {            JSONObject json1 = new JSONObject();              json1.put("companyname","测试2017082902");                    json1.put("companypassword","88888888a");               HttpClientUtil httpClientUtil = new HttpClientUtil();            String httpOrgCreateTestRtn = httpClientUtil.doPost(method,json1,"utf-8");              if(httpOrgCreateTestRtn != null)            {                System.out.println("返回结果: " +httpOrgCreateTestRtn);                JSONObject json = new JSONObject(httpOrgCreateTestRtn);                if(!json.isNull("code"))                {                    String code = json.getString("code");                    String status = json.getString("status");                    if(code.equals("1"))                    {                        String companycode = json.getString("companycode");                        System.out.println("维修企业编码:" + companycode);                    }else                    {                        System.out.println("未通过原因:" + status);                    }                }            }        }        catch (Exception e)        {            e.printStackTrace();        }    }

结果示例

返回结果: {"status":"新增成功","code":"1"}