java - 从 HttpClient 获取 httpresponse

标签 java http

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

相关文章:

在 JBOSS 上使用 CXF 处理 SOAP 请求时出现 java.lang.ClassNotFoundException

http - 如何结合使用 defer 和 http.ListenAndServe?

security - HTTPS 是否可以防止 session 骑马?

java - TextField 中的值抛出 java.lang.NumberFormatException 错误

java - JFlex 中的多行注释 - EOF 错误

java.lang.ClassException : A cannot be cast into B 异常

JavaFx 自定义设计按钮在使用的应用程序中单击检测

spring - Grails可运行jar中的Spring Security重定向到HTTP

http - 发送复杂对象 GET 与 POST

http - python 在 Content-Disposition 中请求 POST Multipart/form-data 和附加参数