setContentType("application/x-www-form-urlencoded")播放框架2.0.x [英] setContentType("application/x-www-form-urlencoded") play framework 2.0.x

查看:171
本文介绍了setContentType("application/x-www-form-urlencoded")播放框架2.0.x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用Web服务,但是在play Framework 2.0.x中,我无法调用任何已编码的Web服务.

I am trying to invoke web service but in play framework 2.0.x i am unable to invoke any web service which is encoded.

我已经研究了代码:

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

参考: https://stackoverflow.com/a/14938117/4410109

我已经在Play框架2.0.8上尝试了上述代码,但出现此错误:

i have tried the above code on play framework 2.0.8 but i am getting this error:

错误:找不到符号.setContentType("application/x-www-form-urlencoded; charset = utf-8")

error: cannot find symbol .setContentType("application/x-www-form-urlencoded; charset=utf-8")

有什么方法可以在播放框架2.0.8中设置内容类型. ?

is there any way to set content type in play framework 2.0.8. ?

谢谢

编辑:

这是我的代码:

Promise<WS.Response> result2 = WS.url("desired-url")
            .setQueryParameter("sUsername","test")
            .setQueryParameter("sPwd","hbl@1234")
            .setQueryParameter("sMobileno","03332560744")
            .setQueryParameter("sTransaction_id","asdfasdfasdfasdfasdf")
            .setQueryParameter("sMessage","test")
            .post("content");

         WS.Response rs = result2.get();

它返回无效格式错误.并且我还使用 form-data 和相同的参数从REST Client中尝试了此操作,它还返回了相同的错误无效格式".但是当我尝试使用 x-www-form-urlencoded 时,它会返回所需的结果.但是在play 2.0中,我无法设置内容类型.

it returns me invalid format error. and i have also tried it from REST Client with form-data and with same parameters, it also returns me same error "Invalid Format". but when i tried it with x-www-form-urlencoded it returns the desired result. but in play 2.0 i cant set the the content Type.

谢谢

推荐答案

在play 2.0中,您可以做到

In play 2.0 you can do

response().setContentType("application/x-www-form-urlencoded; charset=utf-8");
return ok(response.toString());

这篇关于setContentType("application/x-www-form-urlencoded")播放框架2.0.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆