【发布时间】:2014-08-27 04:54:03
【问题描述】:
我正在尝试使用来自 android 应用程序的 apache HttpClient 向服务器发出请求,但我得到的响应没有标头 Set-Cookies。 我尝试在桌面应用程序上做同样的事情并且它有效,我收到了所有标题。 我究竟做错了什么?帮帮我,请解决这个问题。
HttpClient httpclient1 = new DefaultHttpClient();
HttpGet req1 = new HttpGet("http://www.example.com");
req1.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
req1.addHeader("Accept-Encoding", "gzip,deflate,sdch");
req1.addHeader("Accept-Language", "ru,en-US;q=0.8,en;q=0.6,uk;q=0.4");
req1.addHeader("Connection", "keep-alive");
req1.addHeader("Host", "www.example.com");
req1.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36");
String cookie = "";
try {
HttpResponse response1 = httpclient1.execute(req1);
Header[] headers = response1.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i] + "");
if (headers[i].toString().startsWith("Set-Cookie: "))
cookie = headers[i].toString().substring(12);
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://www.example.com/1.php");
postRequest.addHeader("Referer", "http://www.example.com/2.php");
if (cookie.length() > 0) {
cookie = cookie.replace(" path=/", "");
postRequest.addHeader("Cookie", cookie);
}
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "my-username"));
nameValuePairs.add(new BasicNameValuePair("password", "my-password"));
postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(postRequest);
Header[] hs = postRequest.getAllHeaders();
for (int i = 0; i < hs.length; i++) {
System.out.println(hs[i] + "");
}
System.out.println(response.getStatusLine().getStatusCode() + "");
Header[] hs3 = response.getAllHeaders();
for (int i = 0; i < hs3.length; i++) {
System.out.println(hs3[i] + "");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
System.err.println("ClientProtocolException");
} catch (IOException e) {
e.printStackTrace();
System.err.println("IOException");
}
这是我使用 android 应用收到的标题:
200
Server: nginx/0.7.67
Date: Sun, 06 Jul 2014 16:25:26 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.3.3-7+squeeze14
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
这是来自桌面应用程序:
302
Server: nginx/0.7.67
Date: Sun, 06 Jul 2014 16:39:55 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.3-7+squeeze14
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: http://www.example.com/
Set-Cookie: uid=12345; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/
Set-Cookie: pass=11111111111111111111111111111111; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/
Vary: Accept-Encoding
Content-Length: 139
【问题讨论】:
-
不确定是否有人有同样的原因,但是当我尝试通过开放的 WiFi 网络使用应用程序时遇到了这个问题,我想这个网络从原始响应中剪切了 Set-Cookie 标头。当我通过移动网络连接时,一切正常。
标签: java android cookies httpclient