2013-07-26 144 views
5

我正在開發一個Android和Java的服務器應用程序。 服務器應用程序在Jetty上運行。 Android應用程序在同一臺計算機上模擬。POST請求變成GET

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

這是處理程序的代碼:

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

我不知道這可能是一個問題,我認爲如果我使用HttpPost,方法類型應該是POST。

+1

什麼是'postGetSalt'? –

+0

這是「發佈」變量的原始名稱。我改變了它,但不幸的是沒有到處。我糾正了。 – szedjani

回答

11

如果可以的話,可否請您發佈完整的處理程序或處理程序初始化。但我會對答案進行猜測。

我有一種感覺,你的POST請求實際上是通過302重定向的,所以處理程序正確地接收它作爲GET請求。

默認情況下,具有「/ app」上下文的Jetty ContextHandler實際上會將任何請求重定向到「/ app」到「/ app /」,看看setAllowNullPathInfo

所以,你有2個可能的解決方案,請致電setAllowNullPathInfo(true)您ContextHandler中,或在客戶端上改變你的帖子的網址,以HttpPost post = new HttpPost("http://10.0.2.2:8080/app/");

它可以幫助您啓用碼頭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; 
    } 
} 

從請求日誌,發佈請求爲 「/應用」,產生了302

[30 /月/ 2013:12:28:09 0000]「POST/HTTP的應用程序/1.1" 302 0

[30 /月/ 2013:12:28:09 0000] 「GET /應用程序/ HTTP/1.1」 200 0

直接請求到 「/應用程序/」:

[30 /月/ 2013:12:28:16 0000] 「POST /應用程序/ HTTP/1.1」 200 0

+1

你的猜測是對的!初始化完全相同,處理程序僅包含此方法。我在Android應用程序的網址末尾添加了一個加號「/」,它可以工作。非常感謝,我無法單獨找到這個問題。 – szedjani

+0

@Andrew你從重新編碼整個HTTP服務器中拯救了我。謝謝。 –

+0

我正在使用Rails應用程序,並且遇到了類似的問題。我發佈到一個被轉發的域,所以'POST'請求正在成爲'GET'請求。 – Skovy