为什么获取请求正在工作,但post不在java servlet中 [英] why get requests are working, but post are not in the java servlets
问题描述
我有一个servlet,我从get请求调用它,它工作正常,但是当我使用这个帖子请求调用它时
I have a servlet, I call it from a get request, it works, good, but when i call it using this post request
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 $上有例外c $ c>,这是:
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
我有什么不对的请求?
注意
我可以给你servlet代码,它很简单,但我虽然因为我可以发出get请求,因此servlet工作正常,问题更可能来自我的post客户端请求。Note
I could give you the servlet code, it is simple, but i though because i can make a get request, so the servlet is working fine and the problem is more likely from my post client request.当我这样做时
httpPost.addHeader("Content-Type", "multipart/related;");
它有效,但当我这样做时:
it works, but when i do this:
httpPost.addHeader("Content-Type", MediaType.TEXT_HTML);
我再次得到了例外。如果客户端请求错误的内容类型,我想返回自定义消息。请帮助
i got the exception again. I want to return a custom message if the client requests a wrong content type. help please
推荐答案
发布您的servlet和web.xml(如果您正在使用它)。 404表示找不到所请求的资源。你缺少doPost吗?
Post your servlet, and your web.xml if you're using one. 404 means that the requested resource was not found. Are you missing doPost?
这意味着 http:// localhost:8080 / ServletExample1 / multipart1
不是有效的POST URL由于某种原因。提出请求的文件,让我们看看他们说了什么。
That means that http://localhost:8080/ServletExample1/multipart1
is not a valid POST URL for some reason. Put up the file(s) requested and let's see what they say.
这篇关于为什么获取请求正在工作,但post不在java servlet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!