POST请求变得GET [英] POST request becomes GET

查看:297
本文介绍了POST请求变得GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android和Java中的服务器应用程序。 服务器应用程序上运行的码头。 Android的应用程序模拟在同一台计算机上。

I am developing an Android and a server application in Java. The server application runs on Jetty. The Android application is emulated on the same computer.

Android的应用程序发送一个POST请求到服务器,但在服务器间preT它作为一个GET的处理程序。

The Android application sends a POST request to the server, but the handler of the server interpret it as a GET.

当我使用的发送HTTP工具的模拟POST请求,它完美(我指的是方法是POST类型)。

When I use Send HTTP Tool to simulate POST request, it works perfectly (I mean the type of the method is POST).

这是Android应用程序的code片段:

This is the code-fragment of the Android application:

HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),
        10000); // Timeout Limit
HttpResponse response;

// Create message
JSONObject json = new JSONObject();
json.put("request_type", "info");
json.put("user_name", mEmail);

// Send message and get response
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
HttpPost post = new HttpPost("http://10.0.2.2:8080/app");
post.setEntity(se);
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json; charset=UTF-8");
response = client.execute(post);

这是处理程序的code:

And this is the code of the handler:

public void handle(String target, Request baseRequest, 
    HttpServletRequest request, HttpServletResponse response) {
    System.out.println(request.getMethod());
}

我不知道这可能是一个问题,因为我觉得如果我使用HttpPost,方法类型应该是POST。

I don't know what could be a problem, as I think if I use HttpPost, the method type should be POST.

推荐答案

如果你能,你能不能请发表您的完整的处理程序,处理程序初始化。但我要带答案的第一个猜测。

If you could, could you please post your complete handler, or the handler initialisation. But I am going to take a guess on the answer.

我有你的POST请求实际上得到通过302重定向的感觉,所以处理程序正确地接收它作为一个GET请求。

I have a feeling that your POST request is actually getting redirected via a 302, so the handler is correctly receiving it as a GET request.

在默认情况下码头的ContextHandler以/应用程序一上下文将重定向任何请求/应用程序,以/程序/,看看<一href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/server/handler/ContextHandler.html#setAllowNullPathInfo%28boolean%29">setAllowNullPathInfo.

By default a Jetty ContextHandler with a context of "/app" will actually redirect any request to "/app" to "/app/", have a look at setAllowNullPathInfo.

所以,你有2个可能的解决方案,呼叫 setAllowNullPathInfo(真)您的ContextHandler,或者改变你的帖子的网址在客户端后HttpPost =新HttpPost(http://10.0.2.2:8080/app/);

So you have 2 possible solutions, call setAllowNullPathInfo(true) on your ContextHandler, or change your post url on the client to HttpPost post = new HttpPost("http://10.0.2.2:8080/app/");

这可以帮助你实现在码头RequestLogs,请参见码头/教程/ RequestLog

It might help you to enable RequestLogs on jetty, see Jetty/Tutorial/RequestLog

使用下面的服务器,你可以看到不同的请求/应用程序,并请求/应用程序之间/经由请求日志。

Using the following server you can see the difference between a request to /app and a request to /app/ via the request log.

public class RequestLogPost {

  public static class PostHandler extends ContextHandler {
    public PostHandler() {
      setContextPath("/app");
      // setAllowNullPathInfo(true); // enable to see difference in request handling
    }

    @Override
    public void doHandle(String target, Request baseRequest, HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {
      System.out.println(request.getMethod());
      response.setStatus(HttpStatus.OK_200);
      baseRequest.setHandled(true);
    }
  }

  public static void main(String[] args) throws Exception {
    Server server = new Server(5555);

    HandlerCollection handlers = new HandlerCollection();
    handlers.addHandler(new PostHandler());
    handlers.addHandler(new DefaultHandler());
    handlers.addHandler(createRequestLogHandler());

    server.setHandler(handlers);

    server.start();
    server.join();
  }

  private static RequestLogHandler createRequestLogHandler() {
    final int RETAIN_FOREVER = 0; // see RolloverFileOutputStream, 0 == forever.
    RequestLogHandler logHandler = new RequestLogHandler();

    NCSARequestLog ncsaRequestLog = new AsyncNCSARequestLog("requests.log");
    ncsaRequestLog.setAppend(true);
    ncsaRequestLog.setExtended(true);
    ncsaRequestLog.setLogTimeZone("GMT");
    ncsaRequestLog.setRetainDays(RETAIN_FOREVER);
    logHandler.setRequestLog(ncsaRequestLog);
    return logHandler;
  }
}

从请求日志,发布请求/应用,产生了302

From the request logs, Post request to "/app", resulting in a 302

[30 /七月/ 2013:12:28:09 +0000]POST /应用程序HTTP / 1.1302 0

[30/Jul/2013:12:28:09 +0000] "POST /app HTTP/1.1" 302 0

[30 /七月/ 2013:12:28:09 +0000]GET / APP / HTTP / 1.1200 0

[30/Jul/2013:12:28:09 +0000] "GET /app/ HTTP/1.1" 200 0

直接请求/程序/:

[30 /七月/ 2013:12:28:16 +0000]POST / APP / HTTP / 1.1200 0

[30/Jul/2013:12:28:16 +0000] "POST /app/ HTTP/1.1" 200 0

这篇关于POST请求变得GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆