gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:14:35 24 4
gpt4 key购买 nike

当我这样做时:

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

最佳答案

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

Header[] h = response.getAllHeaders();

您还可以检查 Response 的状态代码并像这样做出相应的响应:

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
}

关于java - 如何获取标题? (java,httpclient 4.X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5846886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com