如何将查询字符串添加到httpwebrequest

use*_*949 12 .net c#

我想在httpwebrequest中添加一些查询字符串,但是我找不到任何属性?我记得有一个QueryString字典,我之前可以使用它.

Rob*_*Rob 17

添加查询字符串的最佳方法如下:

var targetUri = new Uri("http://www.example.org?queryString=a&b=c");
var webRequest = (HttpWebRequest)WebRequest.Create(targetUri);

var webRequestResponse = webRequest.GetResponse();
Run Code Online (Sandbox Code Playgroud)

请记住: 如果您正在使用用户输入来构建Uri,请确保对其进行验证,将其转义并且不信任它.

  • @ShashwatTripathi,因为`RequestUri`属性是只读的,并且`Request.Uri`上的`Query`属性也是如此,我怀疑答案是你不能. (2认同)