使用GWT中的参数发出POST请求
我正在尝试使用一组参数向给定URL发送POST请求。我遇到的问题是发出POST请求,但没有传递任何参数使用GWT中的参数发出POST请求,gwt,smartgwt,Gwt,Smartgwt,我正在尝试使用一组参数向给定URL发送POST请求。我遇到的问题是发出POST请求,但没有传递任何参数 RequestBuilder=newrequestbuilder(RequestBuilder.POST,url); StringBuilder sb=新的StringBuilder(); for(字符串k:parmsRequest.keySet()){ 字符串vx=URL.encodeComponent(parmsRequest.get(k)); 如果(sb.length()>0){ 某人附
RequestBuilder=newrequestbuilder(RequestBuilder.POST,url);
StringBuilder sb=新的StringBuilder();
for(字符串k:parmsRequest.keySet()){
字符串vx=URL.encodeComponent(parmsRequest.get(k));
如果(sb.length()>0){
某人附加(“&”);
}
sb.追加(k).追加(“=”).追加(vx);
}
试一试{
Request response=builder.sendRequest(sb.toString(),new RequestCallback()){
public void onError(请求,可丢弃的异常){}
接收到公共void onResponseReceived(请求,响应){}
});
}捕获(请求异常e){}
}
如果我使用模式GET并手动将查询字符串添加到请求中,这很好,但我需要使用POST,因为要传递的数据可能很大……设置请求的标题:
builder.setHeader("Content-type", "application/x-www-form-urlencoded");
web表单不能用于向混合使用GET和POST的页面发送请求。如果将表单的方法设置为GET,则所有参数都在查询字符串中。如果将表单的方法设置为POST,则所有参数都在请求正文中
来源:HTML4.01标准,第17.13节表单提交url:这应该已经起作用了-但是当使用POST时,您必须在Servlet中以不同的方式读取提交的数据(我假设您在服务器端使用Java?) 您可以使用如下Servlet进行尝试:
公共类MyServlet扩展了HttpServlet{
@凌驾
受保护的void doPost(HttpServletRequest-req、HttpServletResponse-resp)
抛出ServletException、IOException{
System.out.println(req.getReader().readLine());
}
}
当然,您可以将req.getReader()
或req.getInputStream()
的内容复制到您自己的缓冲区或字符串等中。我的建议是:
放弃参数方法
改用@RequestBody。它干净多了。
@RequestParam仅在您希望对服务器执行GET请求以快速测试rest服务时才有用。
如果您要处理任何复杂程度的数据,最好使用对服务器的POST请求,这些请求没有最大内容限制
下面是一个如何向服务器发送请求的示例。
注意:在这种情况下,如果使用springboot作为后端,则必须操作内容类型fo be 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);
}
}
private void invokeRestService(){
试一试{
//(a)准备对服务器的JSON请求
RequestBuilder=newrequestbuilder(RequestBuilder.POST,JSON_URL);
//使内容类型与SpringBoot的Exportations兼容
//RESTWeb服务
setHeader(“内容类型”,“应用程序/json;字符集=UTF-8”);
//(b)准备请求对象
UserLoginGwtRpcMessageOverlay jsonRequest=UserLoginGwtRpcMessageOverlay.create();
jsonRequest.setUserName(“John777”);
setHashedPassword(“lalal”);
字符串jsonRequestStr=JsonUtils.stringify(jsonRequest);
//(c)发送HTTP Json请求
Request-Request=builder.sendRequest(jsonRequestStr,newrequestcallback()){
//(i)出现错误时的回调处理程序
公共void onError(请求,可丢弃异常){
log(Level.SEVERE,“无法检索JSON”,异常);
}
//(二)成功回访结果
接收到公共无效onResponseReceived(请求-请求,响应-响应){
if(200==response.getStatusCode()){
UserLoginGwtRpcMessageOverlay响应overlay=JsonUtils
.safeEval(response.getText());
LOGGER.info(“responseOverlay:+responseOverlay.getUserName());
}否则{
log(Level.SEVERE,“无法检索JSON(“+response.getStatusText()+”));
}
}
});
}捕获(请求异常e){
LOGGER.log(Level.severy,“无法执行请求”,e);
}
}
注意UserLoginGwtRpcMessageOverlay是一个补丁。这不是一个GwtRpc可序列化对象,而是一个扩展gwt javascript对象的类
问候。谢谢!我的acceptor servlet在没有。。。。我从没想过!谢谢