关于HttpsURLConnection自动重试导致的请求重复

[问题描述] 

        在业务中与第三方对接时,在11秒log显示开始发起请求,24秒接受到请求结果,显示“请求序列号重复,请求失败”。查询log确认这段程序只触发一次,对方给出的日志显示第一次请求11秒接收,14秒给出返回结果,23秒收到同一序列号的请求,给出请求失败的response。

[代码段] 

        

/**
   * 向指定URL发送POST方法的请求
   *
   * @return URL所代表远程资源的响应
   * @throws Exception
   */
  public static String sendPost(String tr1XML, String postUrl, String authString, int timeOutSecond, KeyManager[] keyManagers) throws Exception {
    log.info("发送tr1报文:{}", tr1XML);
    ParseXMLUtil parseUtil = new ParseXMLUtil();
    OutputStream out = null;
    String reqData = null;

    try {
      HttpsURLConnection conn = (HttpsURLConnection) createConnection(postUrl, authString, timeOutSecond, keyManagers);
      // 获取URLConnection对象对应的输出流
      out = conn.getOutputStream();
      //发送请求参数
      out.write(tr1XML.getByt
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpsURLConnectionHttpURLConnection 的一个子类,用于在 Java 中进行 https 请求。与 HttpURLConnection 类似,HttpsURLConnection 也提供了对 http 请求的支持,并且可以通过 setRequestMethod() 方法设置请求方法(如 GET、POST 等)。不同的是,HttpsURLConnection 需要进行 SSL/TLS 握手,以确保通信的安全性。 以下是一个简单的示例,演示如何使用 HttpsURLConnection 发送 https 请求: ```java import javax.net.ssl.HttpsURLConnection; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class HttpsExample { public static void main(String[] args) throws Exception { String httpsUrl = "https://example.com"; URL url = new URL(httpsUrl); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); // 可以设置请求方法、超时时间等参数 con.setRequestMethod("GET"); con.setConnectTimeout(5000); con.setReadTimeout(5000); // 打印服务器返回的状态码 System.out.println("Response code: " + con.getResponseCode()); // 读取服务器返回的数据 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); // 打印服务器返回的数据 System.out.println("Response content: " + content.toString()); } } ``` 需要注意的是,在实际使用中,可能需要设置 SSLContext、TrustManager 等参数,以确保 https 请求的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值