从org.apache.http.HttpResponse获取HTTP代码


82

org.apache.http.HttpResponse在Java应用程序中使用该类,因此我需要能够获取HTTP状态代码。如果使用.toString()它,则可以在其中看到HTTP状态代码。我还有其他功能可以以int或String形式获取HTTP状态代码吗?

谢谢一群!

Answers:



69

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



2

一个例子如下

        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());
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.