I want to add some querystrings to httpwebrequest, however I cannot find any property? I remembered there is a QueryString dictionary which I can use before.
1 Answer
The best way to add a query string is as follows:
var targetUri = new Uri("http://www.example.org?queryString=a&b=c");
var webRequest = (HttpWebRequest)WebRequest.Create(targetUri);
var webRequestResponse = webRequest.GetResponse();
Remember: If you're using user input to construct the Uri, ensure that you validate it, escape it and don't trust it.
3 Comments
shashwat
but what if I want to add query string later on..?? e.g
HttpWebRequestObject is already initialized and I want to add something to Uri for query string @Rob don't forget to tag in comment responseRob
@ShashwatTripathi, as the
RequestUri property is read-only and the Query property on Request.Uri is as well, I suspect the answer is that you can't.The Senator
I could't find a way of extending an existing Request so ended up having to re-create the request from scratch if I wanted to append to it.