我有一个示例Java程序,该程序连接到特定的Jenkins URL连接并调用一个API。该程序已在几天前开始运行,但突然开始为所有呼叫显示SocketException Connection Reset
。我无法弄清楚Jenkins Config更改了什么配置。
注意:以下代码段适用于除Jenkins设置以外的所有其他URL。可能出了什么问题?甚至我也可以通过邮递员调用API。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static String getReq(String URL, String serviceName) {
StringBuffer response = new StringBuffer();
try {
URL obj = new URL(
"https://jenkins.abcdef.com/cid/jobs/abc/config.xml");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET"); //$NON-NLS-1$
con.setRequestProperty("User-Agent", "Mozilla/5.0"); //$NON-NLS-1$ //$NON-NLS-2$
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return response.toString();
}
public static void main(String[] args) {
getReq(null, null);
}
}
答案 0 :(得分:0)
似乎您缺少一个:
con.connect();
在此行之前
int responseCode = con.getResponseCode();