我在我的Java应用程序中使用了org.apache.http.HttpResponse
类,我需要能够获取HTTP状态代码。如果我使用了.toString()
,我可以在那里看到HTTP状态码。是否有任何其他函数可以将HTTP状态代码作为int或String?从org.apache.http.HttpResponse获取HTTP代码
非常感谢!
我在我的Java应用程序中使用了org.apache.http.HttpResponse
类,我需要能够获取HTTP状态代码。如果我使用了.toString()
,我可以在那里看到HTTP状态码。是否有任何其他函数可以将HTTP状态代码作为int或String?从org.apache.http.HttpResponse获取HTTP代码
非常感谢!
使用HttpResponse.getStatusLine()
,它返回一个包含状态码,协议版本和“reason”的StatusLine
对象。
httpResponse.getStatusLine().getStatusCode()
我已经使用httpResponse.getStatusLine().getStatusCode()
,并且已经发现这可以返回整数http状态码。
甲示例将是如下面,
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());