Get HTTP code from org.apache.http.HttpResponse

后端 未结 4 1683
执笔经年
执笔经年 2021-02-03 16:38

I\'m using the org.apache.http.HttpResponse class in my Java application, and I need to be able to get the HTTP status code. If I used .toString() on i

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-03 17:05

    A example will be as below,

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

提交回复
热议问题