2010-10-21 79 views
16

我正嘗試使用一組參數對給定的URL執行POST請求。我遇到的問題是發出POST請求,但沒有參數傳遞。使用GWT中的參數發出POST請求

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url); 

    StringBuilder sb = new StringBuilder(); 
    for (String k: parmsRequest.keySet()) { 
     String vx = URL.encodeComponent(parmsRequest.get(k)); 
     if (sb.length() > 0) { 
      sb.append("&"); 
     } 
     sb.append(k).append("=").append(vx); 
    } 

    try { 
     Request response = builder.sendRequest(sb.toString(), new RequestCallback() { 

      public void onError(Request request, Throwable exception) {} 

      public void onResponseReceived(Request request, Response response) {} 
     }); 
    } catch (RequestException e) {} 
} 

,如果我使用模式GET和手動查詢字符串添加到請求這只是正常 - 但我需要使用POST作爲傳承下去可能是大數據....

回答

24

套裝請求標題:

builder.setHeader("Content-type", "application/x-www-form-urlencoded"); 
+1

謝謝!我的接受者servlet不工作沒有....從來沒有想過這個!謝謝! – Lenz 2010-10-21 13:11:51

+0

很高興我能幫忙! – z00bs 2010-10-21 13:16:28

0

Web窗體無法用於向使用GET和POST混合的頁面發送請求。如果將表單的方法設置爲GET,則所有參數都位於查詢字符串中。如果您將表單的方法設置爲POST,則所有參數都位於請求正文中。

來源:HTML 4.01標準,部分17.13表單提交網址:http://www.w3.org/TR/html4/interact/forms.html#submit-format

1

這應該已經工作 - 但使用POST的時候,你必須在你的servlet不同讀取提交的數據(我假設,你」 ?

public class MyServlet extends HttpServlet { 

    @Override 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException { 

     System.out.println(req.getReader().readLine()); 
    } 
} 

當然,你也可以的req.getReader()req.getInputStream()的內容複製到自己的b:重新在服務器端使用Java)

你可以用這樣一個Servlet嘗試鞋子或字符串等

0

我的建議是: 刪除參數的方法。

改爲使用@RequestBody。它更乾淨。 @RequestParam僅在您希望對服務器執行GET請求以快速測試其餘服務時纔有用。 如果您正在處理任何複雜程度的數據,則最好將POST請求用於沒有最大內容限制的服務器。

下面是如何將請求傳輸到服務器的示例。 注意:在這種情況下,如果您使用springboot作爲後端,您將不得不操作application/json的內容類型。

private void invokeRestService() { 
     try { 
      // (a) prepare the JSON request to the server 
      RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, JSON_URL); 
      // Make content type compatible with expetations from SpringBoot 
      // rest web service 
      builder.setHeader("Content-Type", "application/json;charset=UTF-8"); 

      // (b) prepare the request object 
      UserLoginGwtRpcMessageOverlay jsonRequest = UserLoginGwtRpcMessageOverlay.create(); 
      jsonRequest.setUserName("John777"); 
      jsonRequest.setHashedPassword("lalal"); 
      String jsonRequestStr = JsonUtils.stringify(jsonRequest); 

      // (c) send an HTTP Json request 
      Request request = builder.sendRequest(jsonRequestStr, new RequestCallback() { 

       // (i) callback handler when there is an error 
       public void onError(Request request, Throwable exception) { 
        LOGGER.log(Level.SEVERE, "Couldn't retrieve JSON", exception); 
       } 

       // (ii) callback result on success 
       public void onResponseReceived(Request request, Response response) { 
        if (200 == response.getStatusCode()) { 
         UserLoginGwtRpcMessageOverlay responseOverlay = JsonUtils 
           .<UserLoginGwtRpcMessageOverlay>safeEval(response.getText()); 
         LOGGER.info("responseOverlay: " + responseOverlay.getUserName()); 
        } else { 
         LOGGER.log(Level.SEVERE, "Couldn't retrieve JSON (" + response.getStatusText() + ")"); 
        } 
       } 
      }); 
     } catch (RequestException e) { 
      LOGGER.log(Level.SEVERE, "Couldn't execute request ", e); 
     } 
    } 

注意UserLoginGwtRpcMessageOverlay是一個補丁工作。這不是一個GwtRpc序列化對象,它是一個擴展gwt javascript對象的類。

問候。