HttpUrlConnection响应代码始终返回-1
我已经在AmazonEC2实例中创建了我的服务器。通过我的android应用程序,我通过HttpUrlConnection连接到服务器。但我得到的响应代码是-1。有人知道吗??这是我的密码。 私有字符串getHttpResponse(最终字符串srcUrl){HttpUrlConnection响应代码始终返回-1,http,httpurlconnection,Http,Httpurlconnection,我已经在AmazonEC2实例中创建了我的服务器。通过我的android应用程序,我通过HttpUrlConnection连接到服务器。但我得到的响应代码是-1。有人知道吗??这是我的密码。 私有字符串getHttpResponse(最终字符串srcUrl){ 这篇文章中的一个答案似乎解决了一些人的问题: 希望这能有所帮助。问题可能是您的headers HTTP版本格式不正确。请检查此SO链接,我在其中回答了一个类似的问题,该问题对我有效 为什么要在GET请求中设置“内容类型”?感谢您的回复.
这篇文章中的一个答案似乎解决了一些人的问题:
希望这能有所帮助。问题可能是您的headers HTTP版本格式不正确。请检查此SO链接,我在其中回答了一个类似的问题,该问题对我有效
为什么要在GET请求中设置“内容类型”?感谢您的回复..我尝试了System.setProperty(“http.keepAlive”,“false”);但遗憾的是,它对我不起作用:(…非常感谢其他帮助。。
HttpURLConnection urlConnection = null;
FileOutputStream fos = null;
try {
URL url = new URL(srcUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Content-Type", "application/json");
mETag = readETagFromPrefForCategory();
urlConnection.setRequestProperty("If-None-Match", mETag);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xyz", "abc".toCharArray());
}
});
urlConnection.connect();
mETag = urlConnection.getHeaderField("ETag");
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
Log.v("http","not modifed");
return readLocalJson();
}
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.w(TAG, "Bad response [" + urlConnection.getResponseCode() + "] from (" + srcUrl + ")");
return null;
}}//end of try block