简体   繁体   English

为什么获取请求有效,但发布不在Java servlet中

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

I have a servlet, I call it from a get request, it works, good, but when i call it using this post request 我有一个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);
    }

I got an exception on the handleResponse , which is: 我在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)

and the status that I print is 404 我打印的状态是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. 我可以给您servlet代码,这很简单,但是我虽然可以发出get请求,但是servlet工作正常,问题出在我的后客户端请求中。

Update 更新资料

when i do this 当我这样做

httpPost.addHeader("Content-Type", MediaType.TEXT_HTML);

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 请帮助

Post your servlet, and your web.xml if you're using one. 发布您的servlet,以及您使用的web.xml。 404 means that the requested resource was not found. 404表示找不到请求的资源。 Are you missing doPost? 您是否缺少doPost?

That means that http://localhost:8080/ServletExample1/multipart1 is not a valid POST URL for some reason. 这意味着http://localhost:8080/ServletExample1/multipart1由于某种原因不是有效的POST URL。 Put up the file(s) requested and let's see what they say. 放置请求的文件,让我们看看他们说了什么。

(take this as a long comment) (以长篇评论)

I know its strange but just try this Method to send Post (change as appropriate) 我知道它很奇怪,但是尝试使用此方法发送Post(适当更改)

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. 我有一个非常相似的问题,我有两个不同的send Post方法,因为一个方法可用于某些服务器,但不能全部用于其他服务器,而另一种方法则可用于其他服务器,第一个方法与您的代码非常相似,第二个(上面的代码)只是发送Post的另一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM