【发布时间】: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