Http介面呼叫示例教程

smileNicky發表於2019-07-27

介紹HttpClient庫的使用前,先介紹jdk裡HttpURLConnection,因為HttpClient是開源的第三方庫,使用方便,不過jdk裡的都是比較基本的,有時候沒有HttpClient的時候也可以使用jdk裡的HttpURLConnection,HttpURLConnection都是調jdk java.net庫的,下面給出例項程式碼:

import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static void main(String[] args) throws Exception {
        String url = "https://ocr-api.ccint.com/ocr_service?app_key=%s";
        String appKey = "xxxxxx"; // your app_key
      String appSecret = "xxxxxx"; // your app_secret
      url = String.format(url, appKey);
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            String imgData = imageToBase64("example.jpg");
            String param="{\"app_secret\":\"%s\",\"image_data\":\"%s\"}";
            param=String.format(param,appSecret,imgData);
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST"); // 設定請求方式
            conn.setRequestProperty("Content-Type", "application/json"); // 設定傳送資料的
            conn.connect();
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.append(param);
            out.flush();
            out.close();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("傳送 POST 請求出現異常!" + e);
            e.printStackTrace();
        }
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println(result);
    }
    public static String imageToBase64(String path)
    {
        String imgFile = path;
        InputStream in = null;
        byte[] data = null;
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
}

然後介紹一下HttpClient,只給出例項程式碼,不封裝成工具類,因為理解基本用法後,自己封裝工具類也是很容易的

HttpClient的GET請求

   CloseableHttpClient httpClient = HttpClients.createDefault();
    //https://github.com/search?utf8=%E2%9C%93&q=jeeplatform&type=
    URIBuilder uriBuilder = new URIBuilder("https://github.com/search");
    uriBuilder.addParameter("q","jeeplatform");
    HttpGet httpGet = new HttpGet(uriBuilder.build());
    CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    if(statusCode==200){
        HttpEntity entity = httpResponse.getEntity();
        System.out.println(EntityUtils.toString(entity,"UTF-8"));
    }
    httpClient.close();
    httpResponse.close();

HttpClient的POST請求,與GET請求類似

    CloseableHttpClient httpClient = HttpClients.createDefault();
    //https://www.sogou.com/sie?query=%E8%8A%B1%E5%8D%83%E9%AA%A8&hdq=AQ7CZ&ekv=3&ie=utf8&
    String uri = "https://www.sogou.com/sie";
    List<NameValuePair> params= new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("query","花千骨"));
    StringEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(entity);
    CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    if(statusCode == 200){
        System.out.println(EntityUtils.toString(httpResponse.getEntity()));
    }
    httpClient.close();
    httpResponse.close();

上面例子是可以支援訪問簽名要求沒那麼高的介面,然後訪問自簽名https的站點,那就要建立一個自定義的SSLContext物件,該物件要有可以儲存信任金鑰的容器,還要有判斷當前連線是否受信任的策略,以及在SSL連線工廠中取消對所有主機名的驗證,如果還是使用預設的HttpClient是會有下面的異常:

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

給出解決方法:

public static CloseableHttpClient getClient() {
        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
        ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
        registryBuilder.register("http", plainSF);
        // 指定信任金鑰儲存物件和連線套接字工廠
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            // 信任任何連結
            TrustStrategy anyTrustStrategy = new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            };
            SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build();
            LayeredConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            registryBuilder.register("https", sslSF);
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        Registry<ConnectionSocketFactory> registry = registryBuilder.build();
        // 設定連線管理器
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT_SECONDS * 1000).setConnectTimeout(TIMEOUT_SECONDS * 1000).build();
        return HttpClientBuilder.create().setConnectionManager(connManager).setMaxConnTotal(POOL_SIZE).setMaxConnPerRoute(POOL_SIZE).setDefaultRequestConfig(requestConfig).build();
    }

然後CloseableHttpClient httpClient = getClient()就可以

然後HttpClient語法相對比較繁雜?如果覺得比較麻煩,可以用Spring框架的RestTemplate,這裡要建立一個自定義的bean,根據需要建立,程式碼示例:

//訪問自簽名https的要點
HttpComponentsClientHttpRequestFactory requestFactory = 
                    new HttpComponentsClientHttpRequestFactory(HttpClientUtil.getClient());
         RestTemplate restTemplate = new RestTemplate(requestFactory);*/
         try {
             //Bean result= restTemplate.getForObject(digitalgdOauthUrl, Bean.class);

相關文章