如何获取标题? (java,httpclient 4.X)

发布于 2024-11-04 01:27:57 字数 936 浏览 2 评论 0原文

当我这样做时:

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();

When I do:

Header[] h = first.getAllHeaders();

The returned Header array is empty. Any ideas? Below is my code.


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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£烟消云散 2024-11-11 01:27:57

您在 first(您的 HttpGet 对象)上调用 getAllHeaders()。您想要在 response 对象上调用 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
}

You're calling getAllHeaders() on first, which is your HttpGet object. You want to call getAllHeaders() on the response object like this:

Header[] h = response.getAllHeaders();

You can also check the Response's status code and respond accordingly like this:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文