HttpsURLConnection中的“已建立连接”异常

时间:2013-08-01 02:40:43

标签: android httpsurlconnection

我正在尝试使用HttpsURLConnection执行POST请求,并获取

java.net.ProtocolException: Connection already established
<{1>} setRequestMethod奇怪的是,conn.connected就在之前返回false

我错过了什么?

URL url = new URL("https://ws.audioscrobbler.com/2.0/");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
// here conn.connected is false
conn.setRequestMethod("POST"); // here I get java.net.ProtocolException: Connection already established
conn.setDoInput(true);

修改

我尝试过DefaultHttpClient并且它可以正常工作,所以我将使用它。

2 个答案:

答案 0 :(得分:1)

对于遇到此问题的其他任何人,我遇到了一个操作顺序问题,当我在请求正文中执行包含内容的HTTP POST时,这只会影响我。在HttpURLConnection实际启动与服务器的连接的所有情况下,并不完全清楚。

我的初始请求看起来像这样:

        HttpURLConnection conn = null;
        try
        {
            conn = (HttpURLConnection) baseUrl.openConnection();  
            conn.setConnectTimeout(connectTimeoutMillis);
            conn.setReadTimeout(requestTimeoutMillis);

            //required for reading a response body
            conn.setDoInput(true);

            //Not all request types have a body (e.g. GET usually doesn't)
            if(requestBody != null && requestBody.length > 0)
            {
                conn.setDoOutput(true);
                conn.setFixedLengthStreamingMode(requestBody.length);
                conn.getOutputStream().write(requestBody);
                conn.getOutputStream().flush();
                conn.getOutputStream().close();
            }

            try
            {
                conn.setRequestMethod(verb.toUpperCase());
            }
            catch (final ProtocolException e)
            {
                response.setError("Invalid HTTP verb \"" + verb + "\" received.","");
                Log.e(TAG, response.errorMessage, e);
                return response;
            }

事实证明,在调用“conn.getOutputStream()”之后你不能调用“conn.setRequestMethod(...)”,所以在我的例子中,简单的修复就是调用“conn.setRequestMethod(。 ..)“在处理写入请求主体之前。工作代码是:

HttpURLConnection conn = null;
        try
        {
            conn = (HttpURLConnection) baseUrl.openConnection();  
            conn.setConnectTimeout(connectTimeoutMillis);
            conn.setReadTimeout(requestTimeoutMillis);

            //required for reading a response body
            conn.setDoInput(true);

            try
            {
                conn.setRequestMethod(verb.toUpperCase());
            }
            catch (final ProtocolException e)
            {
                response.setError("Invalid HTTP verb \"" + verb + "\" received.","");
                Log.e(TAG, response.errorMessage, e);
                return response;
            }

            //Not all request types have a body (e.g. GET usually doesn't)
            if(requestBody != null && requestBody.length > 0)
            {
                conn.setDoOutput(true);
                conn.setFixedLengthStreamingMode(requestBody.length);
                conn.getOutputStream().write(requestBody);
                conn.getOutputStream().flush();
                conn.getOutputStream().close();
            }

唯一真正的变化就是切换调用的顺序并摆脱异常。希望能为遇到此问题的其他人解决问题。

答案 1 :(得分:0)

我尝试DefaultHttpClient并且它有效,所以我会改用它。