2011-05-27 61 views
58

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

非常感谢!

回答

25
httpResponse.getStatusLine().getStatusCode() 
58

我已经使用httpResponse.getStatusLine().getStatusCode(),并且已经发现这可以返回整数http状态码。

1

甲示例将是如下面,

 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());