如何传递斜线和其他“敏感网址”字符到WCF REST服务? [英] How can I pass slash and other 'url sensitive' characters to a WCF REST service?

查看:204
本文介绍了如何传递斜线和其他“敏感网址”字符到WCF REST服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有获得一个参数作为一个字符串的方法的REST WCF服务。此字符串可以包含斜线/字符。它使我的请求错误的,因为我觉得URL出错。

I have a REST WCF service that has a method that gets a parameter as a string. This string can contain slash / character. It makes my request wrong, as I think the URL goes wrong.

当请求,并得到响应(WebRequest.GetResponse())抛出远程服务器返回错误:(400)错误的请求 。例外

When requesting it and getting response (WebRequest.GetResponse()) throws "The remote server returns an error: (400) Bad Request." exception.

我的要求:的http://本地主机:17679 / TestMethod的/ DMC / TCGlOLz1EbEwqAls5Q == \\\
h2cQzTizSBg =

My request: http://localhost:17679/testmethod/DmC/TCGlOLz1EbEwqAls5Q==\nh2cQzTizSBg=

我试图用Uri.EscapeDataString,但它并没有帮助,我得到的例外,因为同以上。
这种转换后我的要求是这样的:
的http://本地主机:17679 / TestMethod的/ DMC%2FTCGlOLz1EbEwqAls5Q%3D%3D%0Ah2cQzTizSBg%3D

I tried to use Uri.EscapeDataString, but it does not help, I get the same exception as above. After this conversion my request looks like this: http://localhost:17679/testmethod/DmC%2FTCGlOLz1EbEwqAls5Q%3D%3D%0Ah2cQzTizSBg%3D

如果我在字符串中传递一个字符串没有斜杠它的作品,因为我想

If I pass a string without slash in the string it works as I want.

如何我可以通过斜线和其他网址敏感字符到WCF REST服务?

How can I pass slash and other 'url sensitive' characters to a WCF REST service?

THX

更新:我解决了它,你可以看到它在我的回答波纹管

推荐答案

我解决它。

URI模板是关键。

如果我定义的URI这种方式,产生上述异常:

If I define URI this way, it produces the exception above:

[OperationContract()]
    [WebGet(UriTemplate = "/testmethod/{testvalue}"/*, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml*/)]
    string TestMethod(string testvalue);



通过修改这种方式,它的工作原理:

By modifying this way, it works:

[OperationContract()]
    [WebGet(UriTemplate = "/testmethod?v={testvalue}"/*, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml*/)]
    string TestMethod(string testvalue);



总之,需要Uri.EscapeDataString!

Anyway, Uri.EscapeDataString is needed!

这篇关于如何传递斜线和其他“敏感网址”字符到WCF REST服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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