我正在使用httpPut到java,我想在执行命令后获取响应代码。它给了我什么是"响应体"是:
HTTP/1.1 200 OK [Cache-Control: no-cache, no-store, must-revalidate, Pragma: no-cache, Expires: Thu, 01 Jan 1970 00:00:00 GMT, Content-Type: application/json, Transfer-Encoding: chunked, Server: Jetty(8.1.8.v20121106)]
但我只想要200!不是整件事。有帮助吗? 这是我的代码:
String url = "http://localhost:80/api/clients/";
String clientID = "1234";
httpclient = new DefaultHttpClient();
HttpPut putRequest = new HttpPut(url + clientID);
putRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
putRequest.setHeader("Charset", "UTF-8");
System.out.println(putRequest);
// Add your data
putRequest.setEntity(new StringEntity(clientID, "UTF-8"));
HttpResponse responseBody = httpclient.execute(putRequest);
答案 0 :(得分:1)
HTTPResonse有一个名为getStatusLine()
的方法,它返回StatusLine
对象。
StatusLine有一个方法getStatusCode()
所以,你需要写的只是
HttpResponse responseBody = httpclient.execute(putRequest);
int resultCode = responseBody.getStatusLine().getStatusCode();//200 in your case
答案 1 :(得分:0)
responseBody.getStatusLine().getStatusCode()
应该为您提供请求的响应代码。