POST请求变成了GET

6

我正在使用Java开发Android和服务器应用程序。

服务器应用程序在Jetty上运行。Android应用程序在同一台计算机上进行仿真。

Android应用程序发送一个POST请求到服务器,但是服务器的处理程序将其解释为GET请求。

当我使用Send HTTP Tool模拟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);

这是处理程序的代码:

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

我不知道问题出在哪里,因为我认为如果我使用HttpPost,方法类型应该是POST。


1
postGetSalt是什么? - JB Nizet
那是“post”变量的原始名称。我修改了它,但不幸的是没有到处都修改。我已经更正了。 - szedjani
1个回答

19
如果可以的话,请您发布完整的处理程序,或者处理程序初始化。但我会猜测答案。
我有一种感觉,你的POST请求实际上是通过302重定向的,所以处理程序正确地将其作为GET请求接收。
默认情况下,Jetty ContextHandler的上下文为"/app",实际上会将任何请求重定向到"/app/",请查看setAllowNullPathInfo
因此,您有两个可能的解决方案:在ContextHandler上调用setAllowNullPathInfo(true),或者在客户端上更改您的post url为HttpPost post = new HttpPost("http://10.0.2.2:8080/app/"); Jetty上启用RequestLogs可能对您有所帮助,请参见Jetty/Tutorial/RequestLog
使用以下服务器,您可以通过请求日志查看对/app和/app/的请求之间的差异。该内容涉及编程。请注意保留HTML标签,不进行解释。
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;
  }
}

根据请求日志,向“/app”发出Post请求,结果为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


2
你猜对了!初始化过程非常相似,处理程序只包含这个方法。我在Android应用程序的URL末尾添加了一个加号“/”符号,现在它可以工作了。非常感谢你,我自己找不到这个问题。 - szedjani
@Andrew,谢谢你帮我避免了重新编写整个HTTP服务器的麻烦。非常感谢! - Alex Spencer
我正在使用Rails应用程序,遇到了类似的问题。我正在向一个被转发的域名发布POST请求,因此该请求变成了GET请求。 - skovy
遇到了同样的问题 - 你的帖子真的很有用,为我提供了一个即时解决方案,否则我无法理解这个问题。(SO是一个非常好的资源) - Tony Eastwood
@skovy,你得到了POST请求变成GET请求的解决方案了吗? - Mayur Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接