hs_bj 2022-07-16 15:47 采纳率: 33.3%
浏览 150

Java调用接口连接超时

问题遇到的现象和发生背景

Java在调用接口,当请求过于频繁时,有的请求会响应超时,有的请求正常。

问题相关代码,请勿粘贴截图

package guide.iface.oa.webservice;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.*;

/**

  •     2022/4/22 新建
    
  • /
    public class OaWebService {

    public static void main(String[] args) throws RemoteException {

      String jsonText = "{'aaa':'1111','bbb':'22222'}";
      getOfficeAuto(jsonText);
    

    }

    /**

    • 默认参数设置 setConnectTimeout:设置连接超时时间,单位毫秒。
    • setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。
    • setSocketTimeout:请求获取数据的超时时间,单位毫秒。访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 暂时定义15分钟
    • /
      private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(600000)
        .setConnectTimeout(600000).setConnectionRequestTimeout(600000).build();
      

    public synchronized static LinkedHashMap<String, String> getOfficeAuto(String jsonText) throws RemoteException {

      String url = "xxxxxxxx";
      String xml = jsonToXml(jsonText);
      return sendHttpPost(url, xml);
    

    }

    /**

    • @Description json 字符串转 xml报文
    • @Param [jsonText]
    • @Return java.lang.String
    • @Date 2022/5/5 9:09
    • /
      public static String jsonToXml(String jsonText) {
      String xml = "";
      xml = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope//%22 xmlns:web="webservices.ofs.weaver.com.cn">\n"
            + "   <soapenv:Header/>\n" + "   <soapenv:Body>\n" + "      <web:receiveRequestInfoByJson>\n"
            + "         <web:in0>" + jsonText + "</web:in0>\n" + "      </web:receiveRequestInfoByJson>\n"
            + "   </soapenv:Body>\n" + "</soapenv:Envelope>";
      
      return xml;
      }

    /**

    • 发送 post请求
    • @param httpUrl 地址
    • @param params 参数(格式:key1=value1&key2=value2)
    • /
      public synchronized static LinkedHashMap<String, String> sendHttpPost(String httpUrl, String params) {
      HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
      try {
        // 设置参数
        StringEntity stringEntity = new StringEntity(params, "UTF-8");
        stringEntity.setContentType("application/json");
        httpPost.setEntity(stringEntity);
      
      } catch (Exception e) {
        e.printStackTrace();
      
      }
      return sendHttpPost(httpPost);
      }

    /**

    • 发送Post请求
    • @param httpPost
    • @return
    • /
      private synchronized static LinkedHashMap<String, String> sendHttpPost(HttpPost httpPost) {
      CloseableHttpClient httpClient = null;
      CloseableHttpResponse response = null;
      HttpEntity entity = null;
      LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
      String responseContent = null;
      try {
        // 创建默认的httpClient实例
        httpClient = HttpClients.createDefault();
        httpPost.setConfig(requestConfig);
        // 执行请求
        response = httpClient.execute(httpPost);
        entity = response.getEntity();
        responseContent = EntityUtils.toString(entity, "UTF-8");// 返回json格式
        map.put("status", response.getStatusLine().getStatusCode() + "");
        map.put("message", StringEscapeUtils.unescapeHtml3(responseContent));
      
      } catch (Exception e) {
        map.put("status", "599");
        if (e.toString().length() > 200) {
            map.put("message", e.toString().substring(0, 200));
        } else {
            map.put("message", e.toString());
        }
        e.printStackTrace();
      
      } finally {
        try {
            // 关闭连接,释放资源
            if (response != null) {
                response.close();
            }
            if (httpClient != null) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
      
      }
      return map;
      }

}

运行结果及报错内容

java.net.UnknownHostException: urlxx
接口响应有时正常,有时连接超时。超时情况大约在集中请求的时候。

我的解答思路和尝试过的方法
  1. 使用ping 未看出有丢包的情况
  2. 尝试使用同步和睡眠方法还是不行
我想要达到的结果

响应无连接超时,数据请求返回正常

  • 写回答

1条回答 默认 最新

  • 程序猿CKeen 2022-07-16 17:06
    关注

    可能别人有限制,你请求频率太高,直接给你返回错误,这种你没办法让别人必须返回正确。
    返回异常自己捕获,自己处理

    评论

报告相同问题?

问题事件

  • 创建了问题 7月16日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line