Java请求遇Connection timed out错误,疑Payload有误,附代码求助
排查Java请求超时问题(java.net.ConnectException: Connection timed out)
我用Java发起请求时碰到了java.net.ConnectException: Connection timed out错误,怀疑是请求Payload创建有问题。我参考下面的Curl请求写了代码,附上我的Java代码,帮忙排查下。
参考的Curl请求
curl --location --request POST 'https://Vishal.net/v1/oauth20/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'grant_type=client_credentials' \ --data-urlencode 'client_secret=d4j8Q~po9Tm3PEShtcHcaeBZW1cuYtHKcbA0FcwG' \ --data-urlencode 'client_id=9572e860-7dd7-45ff-a9c7-e9c09a915a4c' \ --data-urlencode 'scope=api://e3981169-cbc9-4e08-aa35-620714eab5bb/.default'
我的Java代码
public String getToken() throws IOException, ServletException{ String output = ""; URL url = null; try { url = new URL(baseUrl+tokenSubpath); createLogFile(baseUrl+tokenSubpath); } catch (MalformedURLException e) { // TODO Auto-generated catch block return"Error: "+ e.toString(); } System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); HttpURLConnection connection = null; try { //If we need to connect through proxy uncomment the below line connection = (HttpURLConnection) url.openConnection(); } catch (IOException e) { // TODO Auto-generated catch block return "Error: "+e.toString(); } connection.setInstanceFollowRedirects(false); connection.setDoOutput(true); // Triggers POST. try { connection.setRequestMethod("POST"); } catch (IOException e) { // TODO Auto-generated catch block return "Error: "+e.toString(); } String Bearer = new String(Base64.getEncoder().encode(authUserPass.getBytes())); //connection.setRequestProperty("Authorization", "Basic "+Bearer); //vishal bedre 8/8/2022 connection.setRequestProperty("Authorization", ""); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Content-Length", ""); //vishal bedre 8/8/2022 connection.setRequestProperty("Host", ""); //vishal bedre 8/8/2022 connection.setRequestProperty("Accept", "*/*"); //vishal bedre 8/8/2022 connection.setUseCaches(false); String urlParameters="grant_type=client_credentials&client_secret=d4j8Q~po9Tm3PEShtcHcaeBZW1cuYtHKcbA0FcwG&client_id=9572e860-7dd7-45ff-a9c7-e9c09a915a4c&scope=api://e3981169-cbc9-4e08-aa35-620714eab5bb/.default"; try(OutputStream os = connection.getOutputStream()) { //os.write(postData); OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8"); //osw.write("grant_type=client_credentials"); osw.write(urlParameters); osw.flush(); osw.close(); os.close(); //don't forget to close the OutputStream connection.connect(); } catch (IOException e) { // TODO Auto-generated catch block return "Error: "+e.toString(); } }
排查方向
- 优先排查网络层面:Connection timed out多数是网络不通导致,先确认Java程序所在机器能访问目标域名
https://Vishal.net,比如在同一机器执行原Curl命令看是否能成功。如果机器需要代理,代码里未配置代理也会触发超时。 - 修正错误的请求头配置:
- 删掉
connection.setRequestProperty("Authorization", "");,空的Authorization头可能被服务器拦截,原Curl请求也未携带该头,无需设置。 - 重复设置了
Accept头,保留一个即可(比如保留application/json)。 - 删掉手动设置的空
Content-Length和Host头,HttpURLConnection会自动处理这两个字段,手动设空值可能引发异常。
- 删掉
- 规范Payload编码:原Curl用了
--data-urlencode对参数编码,Java代码里直接拼接参数可能存在编码隐患,建议对每个参数值单独用URLEncoder.encode(参数值, "UTF-8")编码后再拼接。 - 清理冗余代码:try-with-resources会自动关闭OutputStream,无需手动调用
os.close()和osw.close();connection.connect()无需手动调用,获取输出流或响应时会自动发起连接。
内容的提问来源于stack exchange,提问作者Vishal Bedre
相关产品推荐
相关产品推荐

