您好我正在使用 HttpClient 发送 Http 请求。我可以调用 Servlet 但返回405 status code.doPost method not allowed
。尝试获得相同的状态码。
而且我也无法得到标题作为回应。我是否需要将请求转发或包含回请求。
//发送Http请求的代码
public void perform(Date now, long remainingRepetitions)
{
log.info("Starting the Job " + now);
System.out.println("Before try 2");
try {
HttpResponse response;
while(true){
HttpClient client = new DefaultHttpClient();
System.out.println("Http Client instantiated");
HttpPost request = new HttpPost("http://localhost:8080/Servlet");
System.out.println("Post Request created");
response = client.execute(request);
System.out.println("Http Status Code = " + response.getStatusLine().getStatusCode() );
Header headers[] = response.getAllHeaders();
for(Header h:headers){
System.out.println("New" +h.getName() + ": " + h.getValue());
}
if(response.getStatusLine().getStatusCode()==200 || response.getStatusLine().getStatusCode()== 405){
if(response.getLastHeader("JobStatus").equals("Success"))
{
break;
}
}
client.getConnectionManager().shutdown();
Thread.sleep(10000);
}
} catch (ClientProtocolException e) {
log.info(e);
} catch (IOException e) {
log.info(e);
} catch (Exception e) {
// TODO Auto-generated catch block
log.info("Exception Occured");
e.printStackTrace();
}finally{
System.out.println("finally");
}
// 小服务程序
private void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
System.out.println("Inside ProcessReqest");
try{
//some method call
resp.addHeader("JobStatus", "Success");
}catch(Exception e){
resp.addHeader("JobStatus", "Failure");
}
}