15

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
  • I don't get you. Just put parameter names and values in the url and execute the request then the page/response will be able to access to the querystring dictionary and consume it. Commented Feb 18, 2011 at 10:44

1 Answer 1

22

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 response
@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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.