My qustion is about the code that should be written in Android using HttpURLConnection to get the response?
The Request and 2 Response as following:
Request [Headers Authorization: ******]
Response
[code: 302, Headers (Content-Type: text/html)]
Response
[code: 302, Headers (Content-Type: text/html)]
The code:
private void makeHttpRequest(URL url) throws 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();
}
}
}
采用
urlConnection.setRequestProperty("Content-type", "text/html");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.