POST请求变为GET

sze*_*ani 5 java android http jetty

我正在用Java开发Android和服务器应用程序.服务器应用程序在Jetty上运行.Android应用程序在同一台计算机上模拟.

Android应用程序向服务器发送POST请求,但服务器的处理程序将其解释为GET.

当我使用发送HTTP工具模拟POST请求时,它完美地工作(我的意思是方法的类型是POST).

这是Android应用程序的代码片段:

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);
Run Code Online (Sandbox Code Playgroud)

这是处理程序的代码:

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

我不知道会出现什么问题,因为我认为如果我使用HttpPost,方法类型应该是POST.

And*_*rew 13

如果可以,请您发布完整的处理程序或处理程序初始化.但我会猜测答案.

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

默认情况下,带有"/ app"上下文的Jetty ContextHandler实际上会将任何请求重定向到"/ app"到"/ app /",看看setAllowNullPathInfo.

因此,您有两种可能的解决方案,调用setAllowNullPathInfo(true)ContextHandler,或将客户端上的帖子更改为HttpPost post = new HttpPost("http://10.0.2.2:8080/app/");

它可能会帮助您在jetty上启用RequestLogs,请参阅Jetty/Tutorial/RequestLog

使用以下服务器,您可以通过请求日志查看/ app请求与/ app /之间的区别.

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

从请求日志中,将请求发送到"/ app",结果为302

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

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

直接请求"/ app /":

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

  • 你的猜测是对的!初始化完全相同,处理程序仅包含此方法。我在 Android 应用程序的 URL 末尾添加了一个加号“/”,并且它有效。非常感谢,我一个人无法找到这个问题。 (2认同)