我有一个servlet,我把它从一个GET请求,它的工作原理,很好,但是当我把它用这个POST请求
private static void doPostToMultiPart() throws URISyntaxException,
ClientProtocolException, IOException {
HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(
"http://localhost:8080/ServletExample1/multipart1");
HttpResponse response = client.execute(httpPost);
System.out.println("response code = "
+ response.getStatusLine().getStatusCode());
String responseString = new BasicResponseHandler()
.handleResponse(response);
System.out.println(responseString);
}
我上了一个异常handleResponse
,那就是:
Exception in thread "main" org.apache.http.client.HttpResponseException: Not Found
at org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:69)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:65)
at com.clients.PostClient1.doPostToMultiPart(PostClient1.java:28)
at com.clients.PostClient1.main(PostClient1.java:16)
而我打印状态为404
错在哪办pleaes?
注意
我可以给你的servlet代码,它是简单的,但我以为,因为我可以做一个GET请求,因此该servlet工作正常,问题是从我的后客户端请求的可能性较大。更新
当我这样做
httpPost.addHeader("Content-Type", "multipart/related;");
它的工作原理,但是当我这样做:
httpPost.addHeader("Content-Type", MediaType.TEXT_HTML);
我再次得到了异常。 我想,如果客户端请求一个错误的内容类型返回自定义消息。 请帮助