why get requests are working, but post are not in the java servlets

半世苍凉 提交于 2019-12-12 00:34:23

问题


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

I got an exception on the handleResponse, which is:

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)

and the status that I print is 404

what wrong do I do pleaes?

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.

Update

when i do this

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


回答1:


(take this as a long comment)

I know its strange but just try this Method to send Post (change as appropriate)

public static String sendPostV2(String data, String url) throws Exception {

    org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
    org.apache.commons.httpclient.methods.PostMethod method = new org.apache.commons.httpclient.methods.PostMethod(url);
    if(data!=null){
       method.addParameter("data", data);
    }
    client.executeMethod(method);
    method.releaseConnection();
    try{
        return method.getResponseBodyAsString();
    }catch (Exception e){
        return null;
    }
}

I had a very similar problem, I have two different send Post method as one works for some server but not all and the other works otherway around, the first one is very similar to your code, and the second one (above code) is just another approach to send Post.




回答2:


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?

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.



来源:https://stackoverflow.com/questions/32650834/why-get-requests-are-working-but-post-are-not-in-the-java-servlets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!