HTTPS接口调用

1.maven工程最外部pom.xml上添加依赖

            
            
                com.squareup.okhttp3
                okhttp
                3.5.0
            

2.maven工程内部模块pom.xml上添加依赖

       
            com.squareup.okhttp3
            okhttp
        

3.Java代码

package com.qa.intl.fulllinktest.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.cert.CertificateException;

import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import javax.net.ssl.*;


public class HTTPSUtils {
    private static final Logger logger = LoggerFactory.getLogger(HTTPSUtils.class.getName());
    public static void main(String[] args) throws Exception {
        OkHttpClient client = new OkHttpClient();
        HttpUrl requestUrl = new HttpUrl.Builder().scheme("https")
                .host("autotest.baidu.com")  //这里是域名
                .addPathSegments("api/autotest/openapi/getLatestStatue") //域名后的路径
                .addQueryParameter("crid", "101111")  //URL?后的参数key value对
                .build();
        Request request = new Request.Builder()
                .url(requestUrl).get()
                .build();
        String result = "";
        try {
            Response response = client.newCall(request).execute();
            System.out.println(request.url());
            if (response.code() == 200) {
                result = response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        client.connectionPool().evictAll();
        System.out.println(result);
    }
}

你可能感兴趣的:(HTTPS接口调用)