gpt4 book ai didi

java - 从 HttpClient 获取 httpresponse

转载 作者:行者123 更新时间:2023-12-01 10:50:57 28 4
gpt4 key购买 nike

有没有办法提取HttpResponse来自HttpClient()GetMethod在java中?我需要HttpEntity来自使用 HttpClient 的响应。有人知道办法吗?我的代码:

client = new HttpClient();
GetMethod get = new GetMethod(URL);
get.setDoAuthentication(true);
try{

int statusCode = client.executeMethod(get);
//parsing XML from response
//System.out.println(get.getResponseBodyAsString());
if(statusCode == 200){
System.out.println();
// here I need HttpResponse object to get HttpEntity
}


}finally{
get.releaseConnection();
}

最佳答案

client = new HttpClient();
GetMethod get = new GetMethod(URL);
get.setDoAuthentication(true);
try{

HttpResponse response = client.executeMethod(get);
//parsing XML from response
//System.out.println(get.getResponseBodyAsString());
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode == 200){
System.out.println();
// here I need HttpResponse object to get HttpEntity
BufferedReader buffer = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = buffer.readLine()) != null) {
textView.append(line);
}
}
} finally {
get.releaseConnection();
}

关于java - 从 HttpClient 获取 httpresponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33912950/

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