我有一个 REST WCF 服务,它有一个获取字符串形式参数的方法。该字符串可以包含斜杠/字符。它使我的请求出错,因为我认为 URL 出错了。
请求它并获得响应时 (WebRequest.GetResponse()) 抛出“远程服务器返回错误:(400) 错误请求。”异常。
我的请求:http://localhost:17679/testmethod/DmC/TCGlOLz1EbEwqAls5Q==\nh2cQzTizSBg=
我尝试使用 Uri.EscapeDataString,但没有帮助,我得到了与上述相同的异常。 转换后我的请求如下所示: http://localhost:17679/testmethod/DmC%2FTCGlOLz1EbEwqAls5Q%3D%3D%0Ah2cQzTizSBg%3D
如果我在字符串中传递一个不带斜线的字符串,它会按我想要的方式工作。
如何将斜杠和其他“url 敏感”字符传递给 WCF REST 服务?
谢谢。
更新:我解决了它,您可以在下面的回答中看到它。
最佳答案
我解决了。
URI 模板是关键。
如果我这样定义 URI,它会产生上面的异常:
[OperationContract()]
[WebGet(UriTemplate = "/testmethod/{testvalue}"/*, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml*/)]
string TestMethod(string testvalue);
这样修改,就可以了:
[OperationContract()]
[WebGet(UriTemplate = "/testmethod?v={testvalue}"/*, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml*/)]
string TestMethod(string testvalue);
无论如何,Uri.EscapeDataString
是必需的!
关于c# - 如何将斜杠和其他 'url sensitive' 字符传递给 WCF REST 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7176726/