webClient.DownloadString中带有方括号的网址 [英] Url with square brackets in webClient.DownloadString

查看:157
本文介绍了webClient.DownloadString中带有方括号的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从需要在url请求中用方括号括起来的网址执行http GET-

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

我尝试将webclient编码设置为utf8和ascii,并且尝试将字符串声明中的方括号分别替换为\ u00b5和\ u00b6来直接对其进行编码,但是这些方法均未发送有效的字符串到服务器.我试过在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...

我可以将引号中的内容精确复制并粘贴到Web浏览器中,并且可以完美运行.我可以使用我需要的代码来请求服务器的功能,这些功能不需要方括号,并且也可以使用.

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?

谢谢!

已更新: 我尝试了建议的代码,都手动包括了转义代码并使用了Uri.EscapeDataString,但仍然失败.每次失败,服务器都会返回一个代码500,如果您在网络浏览器的网址栏中输入错误的请求,则返回的代码与此相同.

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.

我认为问题在于服务器希望这些字符不会转义,并且甚至在我开始尝试在您的帮助下手动进行操作之前,webclient可能已经在自动将它们转换了吗?有没有一种方法可以让webclient完全按原样发送字符串而不进行编码?还是有.encoding属性的设置,可能将[和]作为平面文本而不是特殊字符代码传输?这就是我在最初的尝试中尝试使用@的方式.

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.

更新2-我加载了Fiddler来分析请求...而我的诊断正确...当我在chrome fiddler中键入url时显示为包括"[stuff]".当我通过c#加载相同的地址时,它显示为'%5Bstuff%5D'.因此,我必须说服c#只是以我键入它的方式发送url请求,而不会试图提供帮助并使其符合网络标准.我不知道该怎么做.但是...我知道我需要做的,就是这样!

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!

推荐答案

网络浏览器会自动对其进行编码,而WebClient则不会.

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

有效的编码URI是"http://address:port/datalink?test.a%5Bsomestuff%5D".要从代码中对其进行转义,请使用其中一种转义方法例如.

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.

这篇关于webClient.DownloadString中带有方括号的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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