Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

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

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.

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

My request: http://localhost:17679/testmethod/DmC/TCGlOLz1EbEwqAls5Q== h2cQzTizSBg=

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.

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

Thx.

UPDATE: I solved it, you can see it in my answer bellow.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved it.

URI template is the key.

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

Anyway, Uri.EscapeDataString is needed!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...