有没有办法提取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/