HttpURLConnection已经连接

时间:2016-08-02 20:17:32

标签: java illegalstateexception httpconnection openstack-swift

我正在尝试复制这个,the copy sentence from openstack swift v1(效果很好):

curl -i $publicURL/GXPrueba/StorageAPI/PruebaStorageCopy.png -X PUT -H "X-Auth-Token:  $token" -H "X-Copy-From: /GXPrueba/StorageAPI/PruebaStorage.png" -H "Content-Length: 0"

像这样:

private void copy(String originContainer, String origin, String destinationContainer, String destination) {
    try {
        URL url = new URL(storageUrl + DELIMITER + destinationContainer + DELIMITER + destination);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("X-Auth-Token", authToken);
        conn.setRequestProperty("X-Copy-From", DELIMITER + originContainer + DELIMITER + origin);
        conn.setRequestProperty("Content-Length", "0");

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            System.err.println("Error while copying the object: " + conn.getResponseCode());
        }
        conn.disconnect();

    } catch (MalformedURLException e) {
        System.err.println("Error while copying the object: " + e.getMessage());
    } catch (IOException e) {
        System.err.println("Error while copying the object: " + e.getMessage());
    }
}

我每次都会在不同的行中获得java.lang.IllegalStateException: Already connected异常。 我已经尝试过我发现的其他解决方案(比如删除setDoInput),但似乎没有任何效果。

这是堆栈跟踪

Exception in thread "main" java.lang.IllegalStateException: Already connected
    at java.net.URLConnection.setDoOutput(URLConnection.java:900)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.setDoOutput(HttpsURLConnectionImpl.java:455)
    at javaapplication3.ExternalProviderBluemix.copy(ExternalProviderBluemix.java:212)
    at javaapplication3.ExternalProviderBluemix.copy(ExternalProviderBluemix.java:202)
    at javaapplication3.JavaApplication3.main(JavaApplication3.java:39)
C:\Users\lsarni\AppData\Local\NetBeans\Cache\8.1\executor-snippets\debug.xml:83: Java returned: 1
BUILD FAILED (total time: 24 seconds)

1 个答案:

答案 0 :(得分:3)

我发现这个问题的解决方案是@Sharcoux posted here,它解释了为什么有时它会正常工作。

因此,要在调试NetBeans时解决此问题,您需要从监视中删除所有使用conn的表达式(例如conn.setDoOutPut()等)。