• 关于httpClient使用ajx链接进行请求时的一些事


    出现此问题主要原因是因为在请求过程中证书的问题;有些客户端并未安装服务器要求的证书,所以会产生此问题,以下是部分报错代码:

    javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(Unknown Source)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:397)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
    at com.otitan.tgs.util.WebServiceUtil.get(WebServiceUtil.java:129)
    at com.otitan.tgs.api.impl.TrainQueryAPIImpl.test(TrainQueryAPIImpl.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    解决办法就是:

    //以下是部分需要引用的jar 包

    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    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.ssl.SSLSocketFactory;
    import org.apache.http.conn.ClientConnectionManager;
    import org.apache.http.conn.scheme.Scheme;
    import org.apache.http.conn.scheme.SchemeRegistry;

    //以下代码是定义HttpClient
    HttpClient client = new DefaultHttpClient();

    //将client 转换

    client = wrapClient(client);

    //以下是wrapClient方法

    /**
    * 获取可信任https链接,以避免不受信任证书出现peer not authenticated异常
    *
    * @param base
    * @return
    */
    public static HttpClient wrapClient(HttpClient base) {
    try {
    SSLContext ctx = SSLContext.getInstance(“TLS”);
    X509TrustManager tm = new X509TrustManager() {
    public void checkClientTrusted(X509Certificate[] xcs,
    String string) {
    }

    public void checkServerTrusted(X509Certificate[] xcs,
    String string) {
    }

    public X509Certificate[] getAcceptedIssuers() {
    return null;
    }
    };
    ctx.init(null, new TrustManager[] { tm }, null);
    SSLSocketFactory ssf = new SSLSocketFactory(ctx);
    ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager ccm = base.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme(“https”, ssf, 443));

    return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
    ex.printStackTrace();
    return null;
    }
    }

    public class HttpClientss {
    public static void main(String[] args) {
    String urls = "https://www.szjsjy.com.cn:8001/jyw/queryZBJieGuoListJuNew.do";
    HttpClient http = new DefaultHttpClient();
    http = wrapClient(http);
    HttpPost hget = new HttpPost(urls);
    System.out.println(hget.getURI());
    ResponseHandler responseHandler = new BasicResponseHandler();
    try {
    String responseBody = http.execute(hget, responseHandler);
    System.out.println("------------------------");
    System.out.println(responseBody);
    System.out.println("-----------------------");
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    http.getConnectionManager().shutdown();
    ;
    }
    }

    /**
    *
    * <p>
    * Title: wrapClient
    * </p>
    * <p>
    * Description: 获取可信任https链接,以避免不受信任证书出现peer not authenticated异常
    * </p>
    *
    * @param http
    * @return
    * @return HttpClient
    * @author Administrator
    * @date 2017年6月29日 上午11:35:33
    */
    private static HttpClient wrapClient(HttpClient http) {

    try {
    SSLContext ctx = SSLContext.getInstance("TLS");
    X509TrustManager tManager = new X509TrustManager() {
    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
    return null;
    }
    };
    ctx.init(null, new TrustManager[] { tManager }, null);
    SSLSocketFactory SSF = new SSLSocketFactory(ctx);
    SSF.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager clientConnectionManager = http.getConnectionManager();
    SchemeRegistry sRegistry = clientConnectionManager.getSchemeRegistry();
    sRegistry.register(new Scheme("https", SSF, 443));
    return new DefaultHttpClient(clientConnectionManager, http.getParams());
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    }

    }

    //以下是client 的使用,可以和正常的client 使用一样了,,get方法使用

    // 定义HttpClient
    HttpClient client = new DefaultHttpClient();
    client = wrapClient(client);
    // 实例化HTTP方法
    HttpGet request = new HttpGet();
    request.setURI(new URI(url));
    HttpResponse response = client.execute(request);
    InputStream is = response.getEntity().getContent();

    //以下是client 的使用,可以和正常的client 使用一样了,,post方法使用

    HttpPost postrequest = new HttpPost(url);
    try {
    if (parameters != null) {
    postrequest.setEntity(parameters);
    }
    HttpResponse postresponse = client.execute(postrequest);
    InputStream is = postresponse.getEntity().getContent();
    return is;
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    throw e;
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    throw e;
    } catch (IOException e) {
    e.printStackTrace();
    throw e;
    } catch (Exception e) {
    e.printStackTrace();
    throw e;
    }

    获取的数据为json格式的字符串

  • 相关阅读:
    操作~拷贝clone()
    属性/css~储存
    属性/css~位置.offset()&offsetParent()&position()&scrollTop()&scrollLeft()
    属性/css~尺寸.height()
    设置,获取,删除cookie方法
    Cookie
    属性/css~css.css()
    Python井字游戏
    Asp.net vNext 学习之路(三)
    使用C#发送Http 请求实现模拟登陆(以博客园为例)
  • 原文地址:https://www.cnblogs.com/1x-zfd50/p/7093602.html
Copyright © 2020-2023  润新知