如何在播放 2.1 Java 中为 WS.post() 设置参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14937710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:56:10  来源:igfitidea点击:

How do I set params for WS.post() in play 2.1 Java

javaplayframework-2.1

提问by jakob

I'm trying to perform a post with play.api.libs.ws.WS but I can't figure out how to set the params, my code:

我正在尝试使用 play.api.libs.ws.WS 执行帖子,但我不知道如何设置参数,我的代码:

Promise<Response> promise = WS.url(Play.application().configuration()
                .getString("sms.service.url")).post();

.posttakes (T body, play.api.http.Writeable wrt, play.api.http.ContentTypeOf ct) but I don't understand how I should pass the params there. The documentation only states:

.post需要 (T body, play.api.http.Writeable wrt, play.api.http.ContentTypeOf ct) 但我不明白我应该如何在那里传递参数。该文件仅说明:

Promise<WS.Response> result = WS.url("http://localhost:9001").post("content");

How do I set the content eg. param1=fooand param2=bar?

我如何设置内容,例如。param1=fooparam2=bar?

回答by hthserhs

Try constructing the request like this:

尝试构建这样的请求:

WS.url("http://localhost:9001")
    .setQueryParameter("param1", "foo")
    .setQueryParameter("param2", "bar")
    .post("content");

The method url(java.lang.String url)returns a WS.WSRequestHolderreference which can be used to modify the original request using chained calls to setQueryParameter.

该方法url(java.lang.String url)返回一个WS.WSRequestHolder引用,该引用可用于使用对 的链接调用来修改原始请求setQueryParameter

回答by jakob

Hmm I guess I should really start looking at the imports!

嗯,我想我真的应该开始研究进口了!

I accidentally used import play.api.libs.ws.WS instead of import play.libs.WS; When using play.libs.WSall the methods such as post(String string) and setContentType(String string) revealed themselves. This is how I did it:

我不小心使用了 import play.api.libs.ws.WS 而不是 import play.libs.WS; 当使用play.libs.WS 时,所有的方法如 post(String string) 和 setContentType(String string) 都会暴露出来。我是这样做的:

import play.Play;
import play.libs.F;
import play.libs.WS;

public static Result wsAction() {
    return async(
        play.libs.WS.url(Play.application().configuration()
            .getString("sms.service.url"))
            .setContentType("application/x-www-form-urlencoded; charset=utf-8")                       
            .post("param1=foo&param2=bar").map(
                new F.Function<WS.Response, Result>() {
                    public Result apply(WS.Response response) {
                       return ok(response.toString());
                    }
                }
            )
        );
    }

回答by Malvolio

The accepted answer is wrong, or at least misleading. The code

公认的答案是错误的,或者至少是误导性的。代码

WS.url("http://localhost:9001")
    .setQueryParameter("param1", "foo")
    .setQueryParameter("param2", "bar")
    .post("content");

will post the string contentto http://localhost:9001/?param1=foo&param2=bar, which is almost certainly not what the OP wanted. What is much more likely to work is

将字符串发布contenthttp://localhost:9001/?param1=foo&param2=bar,这几乎肯定不是 OP 想要的。更有可能起作用的是

WS.url("http://localhost:9001")
   .post(Map("param1" -> Seq("foo"),
             "param2" -> Seq("bar")))

which posts the formparam1=foo&param2=barto the the URL http://localhost:9001, which is typically what the server wants.

它将表单发布param1=foo&param2=bar到 URL http://localhost:9001,这通常是服务器想要的。

回答by Aaron Peng

WS.url(url)
.setContentType("application/x-www-form-urlencoded")
.post("param1=foo&param2=bar");

This method uses an HTTP POST method to send its form request. As seen from the official documentation of Play, you should had already known of the GET method.

此方法使用 HTTP POST 方法发送其表单请求。从 Play 的官方文档中可以看出,您应该已经知道 GET 方法。

这种方式是使用post方式提交表单请求,见于play的官方文档,get方式的你应该已经知道了。

这种方式是使用post方式提交表单请求,见于播放的官方文档,获取方式的你应该已经知道了。

回答by SilentDirge

You need to pass in something that can be converted to serialized JSON. This works for me:

您需要传入可以转换为序列化 JSON 的内容。这对我有用:

WS.url("https://www.someurl.com")
  .post(JsObject(Seq("theString" -> JsString(someString))))

The sequence takes any number of JsValues which can also be nested JsObjects.

该序列采用任意数量的 JsValues,这些 JsValues 也可以是嵌套的 JsObjects。

回答by Amol Gaikwad

The right way of doing the blocking request in play 2.1 is

在 play 2.1 中执行阻塞请求的正确方法是

WSRequestHolder wsreqHolder = WS.url("<SOME URL WHICH TAKES PARAMETER>");
wsreqHolder.setQueryParameter("id", "100");
F.Promise<WS.Response> promiseOfResult = wsreqHolder.get();

WS.Response response = promiseOfResult.get(); //block here

String jsonData =  response.getBody();
return ok("Client:"+jsonData);

I have tried it. It works

我试过了。有用