简体   繁体   English

play框架 - 如何在play框架中使用java代码发送post请求

[英]play framework- how to send post request using java code in play framework

Currently I'm switching to play framework to develop but I'm new to this wonderful framework. 目前我正在转向开发框架来开发,但我是这个奇妙框架的新手。 I just want to send a post request to remote server and get response. 我只想向远程服务器发送一个帖子请求并获得响应。

If I use Jersey, it would be quite easy, just like this: 如果我使用泽西岛,那将很容易,就像这样:

WebResource resource = client.resource("http://myfirstUrl");
 resource.addFilter(new LoggingFilter());
 Form form = new Form();
 form.add("grant_type", "authorization_code");
 form.add("client_id", "myclientId");
 form.add("client_secret", "mysecret");
 form.add("code", "mycode");
 form.add("redirect_uri", "http://mysecondUrl");       
 String msg = resource.accept(MediaType.APPLICATION_JSON).post(String.class, form);

and then I can get the msg which is what I want. 然后我可以得到我想要的消息。

But in Play framework, I cannot find any libs to send such post request. 但是在Play框架中,我找不到任何libs来发送这样的帖子请求。 I believe this should be a very simple feature and Play should have integrated it. 我相信这应该是一个非常简单的功能,Play应该集成它。 I've tried to search and found most use case are about the Form in view leve. 我试图搜索并发现大多数用例都是关于视图级别的表单。 Could anyone give me some help or examples? 谁能给我一些帮助或例子? Thanks in advance! 提前致谢!

You can use Play WS API for making asynchronous HTTP Calls within your Play application. 您可以使用Play WS API在Play应用程序中进行异步HTTP调用。 First you should add javaWs as a dependency. 首先,您应该添加javaWs作为依赖项。

libraryDependencies ++= Seq(
  javaWs
)

Then making HTTP POST Requests are as simple as; 然后制作HTTP POST请求就像这样简单;

WS.url("http://myposttarget.com")
 .setContentType("application/x-www-form-urlencoded")
 .post("key1=value1&key2=value2");

post() and other http methods returns a F.Promise<WSResponse> object which is something inherited from Play Scala to Play Java. post()和其他http方法返回一个F.Promise<WSResponse>对象,这是从Play Scala继承到Play Java的东西。 Basically it is the underlying mechanism of asynchronous calls. 基本上它是异步调用的基础机制。 You can process and get the result of your request as follows: 您可以按如下方式处理和获取请求的结果:

Promise<String> promise = WS.url("http://myposttarget.com")
 .setContentType("application/x-www-form-urlencoded")
 .post("key1=value1&key2=value2")
 .map(
    new Function<WSResponse, String>() {
        public String apply(WSResponse response) {
            String result = response.getBody();
            return result;
        }
    }
);

Finally obtained promise object is a wrapper around a String object in our case. 最后获得的promise对象是我们案例中String对象的包装器。 And you can get the wrapped String as: 你可以把包装好的String作为:

long timeout = 1000l;// 1 sec might be too many for most cases!
String result = promise.get(timeout);

timeout is the waiting time until this asynchronous request will be considered as failed. timeout是等待此异步请求被视为失败的等待时间。

For much more detailed explanation and more advanced use cases checkout the documentation and javadocs. 有关更详细的说明和更高级的用例,请查看文档和javadoc。

https://www.playframework.com/documentation/2.3.x/JavaWS https://www.playframework.com/documentation/2.3.x/JavaWS

https://www.playframework.com/documentation/2.3.x/api/java/index.html https://www.playframework.com/documentation/2.3.x/api/java/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM