当我做:

Header[] h = first.getAllHeaders();

返回的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();

最佳答案

您正在getAllHeaders()上调用first,这是您的HttpGet对象。您想像这样在响应对象上调用getAllHeaders():

Header[] h = response.getAllHeaders();

您还可以检查“响应”的状态码,并进行相应的响应,如下所示:
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
}

10-01 12:31