【问题标题】:getting the whole response from HttpClient PostMethod从 HttpClient PostMethod 获取整个响应
【发布时间】:2013-09-23 11:03:43
【问题描述】:

我连接后的方法如下:

try {
            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(VERIFY_PAYMENT_ACTIONURL);

            // key is the parameter
            // MERCHANT_KEY is the value
            method.addParameter("key", MERCHANT_KEY.trim());
            method.addParameter("command", VERIFY_PAYMENT_COMMAND.trim());
            method.addParameter("hash", hash);
            method.addParameter("var1", transactionID);
            method.addParameter("salt", ALGORIHTM_SHA512_SALT_KEY.trim());

            int statusCode = client.executeMethod(method);

        } catch (Exception e) {
            e.printStackTrace();
        }

这工作正常,我得到200 作为状态代码,因为该方法是成功的。我希望得到整个响应,因为我必须根据返回的响应执行更多功能。

来自服务器的响应是如下数组:

array('status' => '1',
'msg' => 'Transaction Fetched Successfully',
'transaction_details' =>
array(
unknown)
)
);

我必须得到 from the response like msg etc 的值。

所以我的问题是如何检索从post call 返回的响应对象

请有人帮我解决这个问题..

【问题讨论】:

    标签: java http-post httpclient httpresponse


    【解决方案1】:

    使用以下任何一种:

    byte[] data = method.getResponseBody();
    String text = method.getResponseBodyAsString();
    InputStream is = method.getResponseBodyAsStream();
    

    当您处理完内容后不要忘记致电method.releaseConnection()(尤其是当您使用method.getResponseBodyAsStream()时)!

    【讨论】:

    • 我使用 method.getResponseBodyAsString ,它打印出我的部分响应,但不是完整的,响应已被切断..
    【解决方案2】:

    HttpClient 更改为org.apache.http.client.HttpClient 界面。

    HttpClient httpClient = new DefaultHttpClient();
    ..
    HttpResponse response = httpClient.execute(...);
    

    在响应对象中,你会找到你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      • 2016-12-20
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多