我正在尝试使用 HttpsURLConnection 做一个 POST 请求,然后得到

java.net.ProtocolException: Connection already established

on 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 并且它有效,所以我将使用它。

最佳答案

对于遇到此问题的任何其他人,我遇到了一个操作顺序问题,该问题仅在我执行请求正文中有内​​容的 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();
            }

唯一真正的变化只是改变了调用的顺序,从而消除了异常。希望这可以为遇到此问题的其他人解决问题。

关于android - HttpsURLConnection 中的 "Connection already established"异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17984368/

10-10 07:30