如何调用别人的接口

原文地址:https://blog.youkuaiyun.com/woshizhangliang999/article/details/47606481

 

最近几天因为项目需求,做一个门禁管理。而门禁信息来源则是妙兜。所以我们这边需要调用妙兜的接口,主要是“设备安装登记接口”和“钥匙凭证发放接口”。

因为之前没有做过“java调用http接口”类似功能,所以在网上找了很多,也比较久。如下代码感觉比较可以,使用过程中也没出什么问题,所以就记录了下来。

    代码如下:

 


 
  1. package com.zhang.miaodou;

  2.  
  3. import java.io.BufferedReader;

  4. import java.io.DataOutputStream;

  5. import java.io.InputStreamReader;

  6. import java.net.HttpURLConnection;

  7. import java.net.URL;

  8. import java.net.URLEncoder;

  9.  
  10. public class DemoTest1 {

  11.  
  12. public static final String GET_URL = "http://112.4.27.9/mall-back/if_user/store_list?storeId=32";

  13. // public static final String POST_URL = "http://112.4.27.9/mall-back/if_user/store_list";

  14. // 妙兜测试接口

  15. public static final String POST_URL = "http://121.40.204.191:8180/mdserver/service/installLock";

  16.  
  17. /**

  18. * 接口调用 GET

  19. */

  20. public static void httpURLConectionGET() {

  21. try {

  22. URL url = new URL(GET_URL); // 把字符串转换为URL请求地址

  23. HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接

  24. connection.connect();// 连接会话

  25. // 获取输入流

  26. BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

  27. String line;

  28. StringBuilder sb = new StringBuilder();

  29. while ((line = br.readLine()) != null) {// 循环读取流

  30. sb.append(line);

  31. }

  32. br.close();// 关闭流

  33. connection.disconnect();// 断开连接

  34. System.out.println(sb.toString());

  35. } catch (Exception e) {

  36. e.printStackTrace();

  37. System.out.println("失败!");

  38. }

  39. }

  40.  
  41. /**

  42. * 接口调用 POST

  43. */

  44. public static void httpURLConnectionPOST () {

  45. try {

  46. URL url = new URL(POST_URL);

  47.  
  48. // 将url 以 open方法返回的urlConnection 连接强转为HttpURLConnection连接 (标识一个url所引用的远程对象连接)

  49. HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此时cnnection只是为一个连接对象,待连接中

  50.  
  51. // 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数)

  52. connection.setDoOutput(true);

  53.  
  54. // 设置连接输入流为true

  55. connection.setDoInput(true);

  56.  
  57. // 设置请求方式为post

  58. connection.setRequestMethod("POST");

  59.  
  60. // post请求缓存设为false

  61. connection.setUseCaches(false);

  62.  
  63. // 设置该HttpURLConnection实例是否自动执行重定向

  64. connection.setInstanceFollowRedirects(true);

  65.  
  66. // 设置请求头里面的各个属性 (以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)

  67. // application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据

  68. // ;charset=utf-8 必须要,不然妙兜那边会出现乱码【★★★★★】

  69. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

  70.  
  71. // 建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)

  72. connection.connect();

  73.  
  74. // 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)

  75. DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());

  76.  
  77. String app_key = "app_key="+ URLEncoder.encode("4f7bf8c8260124e6e9c6bf094951a111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】

  78. String agt_num = "&agt_num="+ URLEncoder.encode("10111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】

  79. String pid = "&pid="+ URLEncoder.encode("BLZXA150401111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】

  80. String departid = "&departid="+ URLEncoder.encode("10007111", "utf-8"); // 已修改【改为错误数据,以免信息泄露】

  81. String install_lock_name = "&install_lock_name="+ URLEncoder.encode("南天大门", "utf-8");

  82. String install_address = "&install_address="+ URLEncoder.encode("北京育新", "utf-8");

  83. String install_gps = "&install_gps="+ URLEncoder.encode("116.350888,40.011001", "utf-8");

  84. String install_work = "&install_work="+ URLEncoder.encode("小李", "utf-8");

  85. String install_telete = "&install_telete="+ URLEncoder.encode("13000000000", "utf-8");

  86. String intall_comm = "&intall_comm="+ URLEncoder.encode("一切正常", "utf-8");

  87.  
  88. // 格式 parm = aaa=111&bbb=222&ccc=333&ddd=444

  89. String parm = app_key+ agt_num+ pid+ departid+ install_lock_name+ install_address+ install_gps+ install_work+ install_telete+ intall_comm;

  90.  
  91. // 将参数输出到连接

  92. dataout.writeBytes(parm);

  93.  
  94. // 输出完成后刷新并关闭流

  95. dataout.flush();

  96. dataout.close(); // 重要且易忽略步骤 (关闭流,切记!)

  97.  
  98. // System.out.println(connection.getResponseCode());

  99.  
  100. // 连接发起请求,处理服务器响应 (从连接获取到输入流并包装为bufferedReader)

  101. BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

  102. String line;

  103. StringBuilder sb = new StringBuilder(); // 用来存储响应数据

  104.  
  105. // 循环读取流,若不到结尾处

  106. while ((line = bf.readLine()) != null) {

  107. // sb.append(bf.readLine());

  108. sb.append(line).append(System.getProperty("line.separator"));

  109. }

  110. bf.close(); // 重要且易忽略步骤 (关闭流,切记!)

  111. connection.disconnect(); // 销毁连接

  112. System.out.println(sb.toString());

  113.  
  114. } catch (Exception e) {

  115. e.printStackTrace();

  116. }

  117. }

  118.  
  119. public static void main(String[] args) {

  120. // httpURLConectionGET();

  121. httpURLConnectionPOST();

  122. }

  123. }



  只使用了POST请求方法,GET没有用,为了保证代码完整性所以没有删除GET请求代码 
    返回结果:

 

 


 
  1. {

  2. "status" : "fail",

  3. "code" : "ERR001",

  4. "msg" : "商户10111不存在"

  5. }

<think>我们正在讨论如何在Java中模拟HTTPS请求来调用外部接口。根据之前的引用,我们可以使用多种方式实现HTTP/HTTPS请求,例如HttpURLConnection、Apache HttpClient、Spring RestTemplate等。由于HTTPS需要处理SSL证书,我们需要确保我们的客户端能够信任服务器的证书,或者忽略证书验证(在开发环境中,生产环境不推荐)。 以下是几种常见的实现方式: 1. 使用HttpURLConnection(Java原生) 2. 使用Apache HttpClient 3. 使用Spring RestTemplate(如果项目使用了Spring) 由于HTTPS涉及到SSL证书,我们需要特别注意证书的验证。在开发环境中,我们可能会使用自签名证书或者测试证书,这时我们可以选择信任所有证书(不推荐在生产环境使用)。生产环境中,我们应该使用正确的证书验证。 下面我将分别展示这三种方式如何实现HTTPS请求(包括忽略证书验证的示例,请注意安全)。 方式一:使用HttpURLConnection(忽略证书验证,仅用于测试) 步骤: a. 创建一个信任所有证书的TrustManager b. 初始化一个SSLContext c. 设置HostnameVerifier,接受所有主机名 d. 打开连接并发送请求 示例代码: ```java import javax.net.ssl.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; public class HttpsTestWithHttpURLConnection { public static void main(String[] args) throws IOException, NoSuchAlgorithmException, KeyManagementException { // 创建信任所有证书的TrustManager TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; // 初始化SSLContext SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // 创建不验证主机名的HostnameVerifier HostnameVerifier allHostsValid = (hostname, session) -> true; HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); // 现在可以发送HTTPS请求 URL url = new URL("https://example.com/api"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); // 读取响应 try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { String line; StringBuilder response = new StringBuilder(); while ((line = in.readLine()) != null) { response.append(line); } System.out.println(response.toString()); } } } ``` 方式二:使用Apache HttpClient(忽略证书验证,仅用于测试) 需要添加依赖(Maven): ```xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> <!-- 使用最新版本 --> </dependency> ``` 示例代码: ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class HttpsTestWithApacheHttpClient { public static void main(String[] args) throws Exception { // 创建信任所有证书的SSLContext SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( sslContextBuilder.build(), NoopHostnameVerifier.INSTANCE); // 创建HttpClient HttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(socketFactory) .build(); // 创建请求 HttpGet request = new HttpGet("https://example.com/api"); // 执行请求 HttpResponse response = httpClient.execute(request); // 处理响应 // ... 读取响应内容 } } ``` 方式三:使用Spring RestTemplate(忽略证书验证,仅用于测试) 如果项目使用Spring,可以使用RestTemplate。同样,我们需要配置一个忽略证书验证的ClientHttpRequestFactory。 示例代码: ```java import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import javax.net.ssl.*; import java.io.IOException; import java.net.HttpURLConnection; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; public class HttpsTestWithRestTemplate { public static void main(String[] args) throws Exception { // 创建自定义的RestTemplate,设置忽略SSL证书 RestTemplate restTemplate = new RestTemplate(new CustomClientHttpRequestFactory()); // 发送请求 ResponseEntity<String> response = restTemplate.exchange( "https://example.com/api", HttpMethod.GET, null, String.class); System.out.println(response.getBody()); } static class CustomClientHttpRequestFactory extends SimpleClientHttpRequestFactory { @Override protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { if (connection instanceof HttpsURLConnection) { ((HttpsURLConnection) connection).setHostnameVerifier((hostname, session) -> true); try { ((HttpsURLConnection) connection).setSSLSocketFactory(createSslSocketFactory()); } catch (Exception ex) { throw new IOException(ex); } } super.prepareConnection(connection, httpMethod); } private SSLSocketFactory createSslSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[]{new X509TrustManager() { public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { } public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}, new java.security.SecureRandom()); return context.getSocketFactory(); } } } ``` 注意:以上示例中忽略证书验证的方式仅适用于开发测试环境,生产环境必须使用正确的证书验证以确保安全。 另外,如果调用的是受信任的CA颁发的证书,则不需要忽略证书验证,直接使用即可。例如,使用HttpURLConnection时,不需要设置自定义的TrustManager和HostnameVerifier。 根据实际需求,可以选择合适的方式。如果项目已经使用了Spring,那么RestTemplate可能是更好的选择。如果需要更灵活的控制,Apache HttpClient提供了丰富的功能。 请根据实际情况整代码,例如设置请求头、请求体、超时时间等。 最后,关于性能,如果我们需要并行调用多个外部接口,可以考虑使用多线程(如CompletableFuture)来并行执行,以缩短总响应时间[^4]。 希望这些信息对您有帮助! §§相关问题§§ 1. 在Java中如何安全地处理HTTPS证书验证(非忽略)? 2. 使用Apache HttpClient如何设置连接超时和读取超时? 3. 如何在Spring Boot项目中配置RestTemplate来调用HTTPS接口? 4. 如何通过多线程并行调用多个HTTPS接口?</think>### Java 模拟 HTTPS 请求调用外部接口Java 中模拟 HTTPS 请求调用外部接口,主要涉及 SSL/TLS 证书处理和 HTTP 客户端的使用。以下是三种常用方法: #### 方法一:使用 HttpURLConnection(原生 Java) ```java import javax.net.ssl.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.security.cert.X509Certificate; public class HttpsCaller { public static void main(String[] args) throws Exception { // 1. 信任所有证书(仅测试环境使用) TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} } }; // 2. 初始化SSL上下文 SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // 3. 创建连接并发送请求 URL url = new URL("https://api.example.com/data"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); // 4. 设置请求方法和超时 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(10000); // 5. 读取响应 try (BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream()))) { String line; while ((line = in.readLine()) != null) { System.out.println(line); } } } } ``` #### 方法二:使用 Apache HttpClient(推荐生产环境) ```java import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import javax.net.ssl.SSLContext; public class ApacheHttpsCaller { public static void main(String[] args) throws Exception { // 1. 创建信任所有证书的SSL上下文 SSLContext sslContext = new SSLContextBuilder() .loadTrustMaterial(null, (cert, authType) -> true).build(); // 2. 创建Socket工厂 SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory( sslContext, NoopHostnameVerifier.INSTANCE); // 3. 创建HTTP客户端 try (CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(factory).build()) { // 4. 创建GET请求 HttpGet request = new HttpGet("https://api.example.com/data"); // 5. 添加请求头 request.addHeader("Content-Type", "application/json"); // 6. 执行请求并获取响应 HttpResponse response = httpClient.execute(request); System.out.println("Status: " + response.getStatusLine()); } } } ``` #### 方法三:使用 Spring RestTemplate ```java import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import javax.net.ssl.*; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; public class RestTemplateCaller { public static void main(String[] args) throws Exception { // 1. 创建信任所有证书的RestTemplate RestTemplate restTemplate = new RestTemplate( new CustomSSLRequestFactory()); // 2. 发送HTTPS请求 ResponseEntity<String> response = restTemplate.getForEntity( "https://api.example.com/data", String.class); System.out.println(response.getBody()); } static class CustomSSLRequestFactory extends SimpleClientHttpRequestFactory { @Override protected void prepareConnection(HttpURLConnection connection, String httpMethod) { if (connection instanceof HttpsURLConnection) { ((HttpsURLConnection) connection).setHostnameVerifier( (hostname, session) -> true); try { ((HttpsURLConnection) connection).setSSLSocketFactory( createSslContext().getSocketFactory()); } catch (Exception e) { /* 处理异常 */ } } super.prepareConnection(connection, httpMethod); } private SSLContext createSslContext() throws NoSuchAlgorithmException, KeyManagementException { SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[]{new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) {} public void checkServerTrusted(X509Certificate[] chain, String authType) {} public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}, null); return context; } } } ``` ### 关键注意事项 1. **证书验证** 上述代码示例均跳过证书验证(`TrustManager`),**仅适用于测试环境**。生产环境应: - 使用受信任的 CA 证书 - 配置证书库:`System.setProperty("javax.net.ssl.trustStore", "path/to/truststore.jks")` 2. **超时设置** 必须设置连接超时和读取超时: ```java // Apache HttpClient 示例 RequestConfig config = RequestConfig.custom() .setConnectTimeout(5000) .setSocketTimeout(10000).build(); httpClient = HttpClients.custom().setDefaultRequestConfig(config).build(); ``` 3. **请求头处理** 根据接口要求添加必要的请求头: ```java request.addHeader("Authorization", "Bearer token123"); request.addHeader("Accept", "application/json"); ``` 4. **异常处理** 必须捕获以下异常: ```java catch (SSLHandshakeException e) { // 证书错误 } catch (ConnectTimeoutException e) { // 连接超时 } catch (SocketTimeoutException e) { // 读取超时 } ``` 5. **性能优化** 对于高频调用: - 使用连接池(如 `PoolingHttpClientConnectionManager`) - 复用 HTTP 客户端实例 - 启用响应压缩 > 提示:生产环境建议使用 Apache HttpClient 或 Spring RestTemplate,它们提供更完善的连接管理和线程安全机制[^1][^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值