我正在创建一个示例应用程序 (java),它收集信息并将该信息发送到 .net 中内置的 Web 服务。通常它工作正常。两个平台都在完美沟通。
但经过一段时间后,我收到了 (java.net.SocketException) java.net.SocketException: Permission denied: connect 错误。
我google了一下,发现JDK7有问题。我只是想确定这只是 JDK7 的问题还是我们可以处理它?</p>
最佳答案
我对客户端使用 HTTPClient,对 http 请求使用 HttpGet/HttpPost 方法。
现在我通过 HttpClientBase 更改客户端代码和 PostMethod/PutMethod 请求代码。这具有释放连接的 releaseConnection() 方法。现在我的代码工作正常。
public void sendHttpPost() throws ClientProtocolException, IOException {
HttpMethodBase httpPostRequest = new PostMethod(url + buildParams());
try {
// add headers
Iterator it = headers.entrySet().iterator();
while (it.hasNext()) {
Entry header = (Entry) it.next();
httpPostRequest.addRequestHeader((String) header.getKey(), (String) header.getValue());
}
((PostMethod) httpPostRequest).setRequestEntity( new StringRequestEntity(new
String(reqEntity), "text/plain", Constants.DEFAULT_ENCODING));
try {
respCode = client.executeMethod(httpPostRequest);
response = httpPostRequest.getResponseBodyAsString();
this.responsePhrase = httpPostRequest.getStatusText();
}catch(Exception ex){
System.out.println("ErrorS "+ex.toString());
} finally {
httpPostRequest.releaseConnection();
}
}catch(Exception ex){
System.out.println("ErrorD "+ex.toString());
}
finally {
}
}
关于java - (java.net.SocketException) java.net.SocketException : Permission denied: connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19789674/