当我做:
Header[] h = first.getAllHeaders();
Run Code Online (Sandbox Code Playgroud)
返回的Header数组为空.有任何想法吗?以下是我的代码.
HttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet first = new HttpGet("http://vk.com");
HttpResponse response = httpclient.execute(first, localContext);
InputStream instream = response.getEntity().getContent();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(instream, Charset.forName("windows-1251")));
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
Header[] h = first.getAllHeaders();
instream.close();
String s = sb.toString();
Run Code Online (Sandbox Code Playgroud)
Jor*_*dan 13
你打电话getAllHeaders()的first,这是你的HTTPGET对象.您要拨打getAllHeaders()的上响应这样的对象:
Header[] h = response.getAllHeaders();
Run Code Online (Sandbox Code Playgroud)
您还可以检查响应的状态代码并做出相应的响应,如下所示:
int statusCode = response.getStatusLine().getStatusCode();
Logger.d("Response returned status code " + statusCode);
if (HttpStatus.SC_OK == statusCode) {
// TODO: handle 200 OK
} else if (HttpStatus.SC_NOT_FOUND == statusCode) {
// TODO: handle 404 Not Found
} else {
// TODO: handle other codes here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14196 次 |
| 最近记录: |