Url with square brackets in webClient.DownloadStri

2019-08-28 09:57发布

I'm trying to perform an http GET from a web address that requires square brackets in the url request - something like :

string requestconstructor = "http://address:port/datalink?test.a[somestuff]"
System.Net.WebClient webClient = new System.Net.WebClient();
string result = webClient.DownloadString(requestconstructor);

I've tried setting webclient encoding to utf8 and ascii, and I've tried replacing the square brackets in the string declaration with \u00b5 and \u00b6 respectively to directly encode them, but none of those methods are sending a valid string to the server. I've tried putting an @ sign in front of requestconstructor in the downloadstring call...

I can copy and paste exactly what is in quotes to a web browser and it works perfectly. I can use the code I've got to make requests to functions of the server that don't requires the square brackets and that also works.

Is there a way to make a request using these characters?

Thank you!

Updated: I tried the suggested code, both manually including escape codes and using Uri.EscapeDataString and it still failed. Each time it's failing with a code 500 returned from the server, which is the same code returned if you mistype the request in the url bar of a web brower.

I think the problem is that the server expects those characters to come NOT escaped and that even before I started trying to do it manually with your help, webclient might have been converting them automatically? Is there a way to get webclient to send the string exactly as it comes in without encoding it? Or is there a setting for the .encoding property that might transmit the [ and ] as plane text instead of special character codes? That's what I was trying to do with the @ in my initial efforts.

Update 2 - I loaded Fiddler to analyze the requests... and I was right in my diagnosis... When I type the url into chrome fiddler shows it as including '[stuff]'. When I load the same address via c# it comes out as '%5Bstuff%5D'. So I've got to convince c# to just send the url request the way I typed it without trying to be helpful and make it comply with the web-standards. I don't know how to do that. But... I know what I need to do, which is something!

2条回答
相关推荐>>
2楼-- · 2019-08-28 10:19

The webbrower is automatically encoding it, the WebClient is not.

A valid encoded URI is "http://address:port/datalink?test.a%5Bsomestuff%5D". To escape it from code, use one of the escape methods eg.

var path = "http://address:port/datalink";
var query = "test.a[somestuff]";
var uri = path + "?" + Uri.EscapeUriString(query);

Using @"" has not effect, as that only affects the string literal parsing.

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-28 10:37

I FINALLY found an ancient post by someone with the same problem! The Uri(string, boolean) constructor lets you promise that you already did all of the escaping, even if you're lying. The working code is here -

 string address = "http://127.0.0.1:8085/datalink?a=stuff[true]";
Uri uri = new Uri(address, true);
WebClient client = new WebClient();
string reply = client.DownloadString(uri);
查看更多
登录 后发表回答