我正在尝试访问网络服务。它在 android 4.4 或 android 5.X 上运行良好。但是当我尝试使用 android 4.1.1 点击“http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID”
时,它总是返回 307 状态代码。但这个网址在 android 4.4 或 5.x 上工作正常。我还尝试点击其他网址,它在 android 4.1.1 上工作正常。
所以请告诉我问题是什么
Log.i(TAG, url);
String response = null;
HttpURLConnection conn = null;
try {
URL webServiceUrl = new URL(url);
conn = (HttpURLConnection) webServiceUrl
.openConnection();
Log.i(TAG, "Connection open");
conn.setRequestMethod(GET);
conn.setConnectTimeout(CONNECTION_TIME_OUT);
conn.setRequestProperty(CONTENT_TYPE, contentType);
conn.setRequestProperty(ACCEPT_TYPE, acceptType);
conn.setDoInput(true);
conn.connect();
Log.i(TAG, "Connection Connected");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK && conn.getInputStream() != null) {
response = StreamUtility.convertStreamToString(conn.getInputStream());
conn.getInputStream().close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return response;
}
最佳答案
将您的 URL 地址替换为 http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID/
(注意 /
结束)。响应代码将为 200
。
更新:
使用您当前的 URL 地址 (http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID
),末尾不带 /
,您可以使用以下代码:
String address = "http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID";
URL url = new URL(address);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
// if print out (debug or logging), you will see secondURL has / at the end
URL secondURL = new URL(urlConnection.getHeaderField("Location"));
HttpURLConnection urlConnection1 = (HttpURLConnection) secondURL.openConnection();
然后使用urlConnection1.getResponseCode()
希望对你有帮助!
关于android - HttpConnectionUrl 始终返回状态代码 307,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759805/