【问题标题】:Get HTTP code from org.apache.http.HttpResponse从 org.apache.http.HttpResponse 获取 HTTP 代码
【发布时间】:2011-09-03 10:35:51
【问题描述】:

我在我的 Java 应用程序中使用 org.apache.http.HttpResponse 类,我需要能够获取 HTTP 状态代码。如果我在上面使用.toString(),我可以在其中看到 HTTP 状态代码。是否有任何其他函数可以让我将 HTTP 状态代码作为 int 或 String 获取?

非常感谢!

【问题讨论】:

    标签: java apache httpresponse


    【解决方案1】:

    使用HttpResponse.getStatusLine(),它返回一个包含状态码、协议版本和“原因”的StatusLine对象。

    【讨论】:

      【解决方案2】:
      httpResponse.getStatusLine().getStatusCode()
      

      【讨论】:

        【解决方案3】:

        我使用了httpResponse.getStatusLine().getStatusCode(),发现它可以可靠地返回整数 http 状态码。

        【讨论】:

          【解决方案4】:

          一个例子如下,

                  final String enhancementPayload ="sunil kumar";
                  HttpPost submitFormReq = new HttpPost("https://bgl-ast/rest/service/form/form-data");
                  StringEntity enhancementJson = new StringEntity(enhancementPayload);
                  submitFormReq.setEntity(enhancementJson);
                  submitFormReq.setHeader("Content-Type", "application/xml");
          
                  HttpResponse response = httpClient.execute( submitFormReq );
                  String result = EntityUtils.toString(response.getEntity());
                  System.out.println("result "+result);
                  assertEquals(200, response.getStatusLine().getStatusCode());
          

          【讨论】:

            猜你喜欢
            • 2012-04-03
            • 1970-01-01
            • 1970-01-01
            • 2017-04-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-14
            • 1970-01-01
            相关资源
            最近更新 更多