HttpURLConnection响应缺少状态行(ProtocolException)

时间:2016-12-28 17:01:42

标签: android http iis httpurlconnection

我目前正在尝试向IIS服务器发出请求(使用HttpURLConnection),该服务器将使用重定向将我弹回目标目的地。

问题:java.net.ProtocolException: Unexpected status line: <html><head><title>Object moved</title></head><body>

调用urlConnection.getInputStream()时会发生这种情况(或调用它的urlConnection.getResponseCode())。

我见过很多案例,这个问题只是一个配置错误的服务器,但在这种情况下,在伪造Android用户代理时向Chrome发出请求会产生预期结果。

此外,服务器是一个古老的服务器,从未成为问题的根源,并且配置方式与生产环境中使用的服务器相同。

到达另一个重定向网页工作正常(使用https://com.google/测试,2次重定向),并且到达服务器上的非重定向网页没有任何问题。

我一直在寻找两天,并没有取得任何进展。任何帮助将不胜感激。

目前使用的代码:

StringBuilder builder = new StringBuilder();
HttpsURLConnection urlConnection = null;

try {

    URL url = new URL(urlStrings[0]);

    urlConnection = (HttpsURLConnection) url.openConnection();

    LogHelper.Log(getBaseContext(), "Request on URL :\n\"" + url + "\"");

    urlConnection.setRequestMethod("GET");
    urlConnection.setReadTimeout(10000);
    urlConnection.setConnectTimeout(15000);
    urlConnection.setRequestProperty("Accept","*/*");

    urlConnection.setDoOutput(false);
    urlConnection.setDoInput(true);

    urlConnection.setInstanceFollowRedirects(true);

    urlConnection.setUseCaches(false);

    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

    urlConnection.connect();

    int status = urlConnection.getResponseCode();
    LogHelper.Log(getBaseContext(), "Status code is \"" + status + "\"");
    if (!url.equals(urlConnection.getURL())) {
        LogHelper.Log(getBaseContext(), "URL after redirection is :\n\"" + urlConnection.getURL() + "\"");
    }

    switch (status/100) {
        case 1:
        case 2:
        case 3:

            InputStream stream = urlConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line).append("\n");
            }
            reader.close();
            break;

        case 4:
        case 5:

            InputStream errorStream = urlConnection.getErrorStream();
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {
                builder.append(errorLine).append("\n");
            }
            errorReader.close();

            throw new Exception(builder.toString());

        default:
            throw new Exception("Unknown status code: " + status);
    }

} catch (Exception e) {
    LogHelper.Log(getBaseContext(), e);
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
    }
}

return builder.toString();

产生的错误:

java.net.ProtocolException: Unexpected status line: <html><head><title>Object moved</title></head><body>
    at com.android.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)
    at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:239)
    at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:104)
    at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:1120)
    at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:951)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(
    at com.pushmanager.clientmdm.activities.RegistrationView$1$2.doInBackground(
    at com.pushmanager.clientmdm.activities.RegistrationView$1$2.doInBackground(
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:0)

原来,这种行为是由于URL字符串中的\n字符引起的。

我不知道okhttp是如何管理这个结果的,但是删除了这个字符后,问题就消失了。