【问题标题】:How to use HttpURLConnection with Response (Content-Type: text/html ) in android?如何在 android 中使用 HttpURLConnection 和 Response (Content-Type: text/html)?
【发布时间】:2017-11-03 02:29:42
【问题描述】:

我的问题是关于应该在 Android 中使用 HttpURLConnection 来获取响应的代码?

请求和2响应如下:

请求 [标题 授权:******]

响应
[代码:302, 标头 (Content-Type: text/html)]

响应
[代码:302, 标头 (Content-Type: text/html)]

代码:

private void makeHttpRequest(URL url) 抛出 IOException {

    String redirectUrl;

    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);
        urlConnection.addRequestProperty("Authorization", "********");
        urlConnection.connect();

        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK
                || urlConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM
                || urlConnection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {

            redirectUrl = urlConnection.getHeaderField("Content-Type");

        } else {
            Log.e(LOG_TAG, "Error redirect response code: " + urlConnection.getResponseCode());
        }

    } catch (IOException e) {
        Log.e(LOG_TAG, "Problem retrieving", e);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

【问题讨论】:

  • 你能检查我的答案吗?谢谢。

标签: android html httpurlconnection


【解决方案1】:

使用

urlConnection.setRequestProperty("Content-type", "text/html"); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2013-06-03
    相关资源
    最近更新 更多