3

我正在创建一个示例应用程序 (java),它收集信息并将该信息发送到 .net 中内置的 Web 服务。通常它工作正常。两个平台都在完美沟通。

但经过一段时间后,我得到 (java.net.SocketException) java.net.SocketException: Permission denied: connect error。

我google了一下,发现JDK7有问题。我只是想确保这只是 JDK7 的问题,或者我们可以处理吗?

4

1 回答 1

1

我将 HTTPClient 用于客户端,将 HttpGet/HttpPost 方法用于 http 请求。

现在我通过 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 {

    }
}
于 2013-11-07T07:18:27.010 回答