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(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);
}
/**
+ " <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;/**
// 设置参数
StringEntity stringEntity = new StringEntity(params, "UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
} catch (Exception e) { e.printStackTrace();
}/**
// 创建默认的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();
}
}}
java.net.UnknownHostException: urlxx
接口响应有时正常,有时连接超时。超时情况大约在集中请求的时候。
响应无连接超时,数据请求返回正常
可能别人有限制,你请求频率太高,直接给你返回错误,这种你没办法让别人必须返回正确。
返回异常自己捕获,自己处理