如何获取标题? (java,httpclient 4.X)
当我这样做时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
first
(您的 HttpGet 对象)上调用getAllHeaders()
。您想要在 response 对象上调用getAllHeaders()
,如下所示:您还可以检查响应的状态代码并做出相应响应,如下所示:
You're calling
getAllHeaders()
onfirst
, which is your HttpGet object. You want to callgetAllHeaders()
on the response object like this:You can also check the Response's status code and respond accordingly like this: