Reputation: 108
I have a webservice that is accepting a string parameter. the parameter will always be a raw url. The user send me a raw url with the username, requestid, the option yes or no. For example:
http://bookReport/request.aspx?user=abc&password=password&request=1&option=yes
I am creating a web request and passing the url as shown below.
var request = (HttpWebRequest)WebRequest.Create(url);
var user = request.RequestUri.Query.ToString();
string[] p = Regex.Split(user, "password=");
string[] password = Regex.Split(p[1], "&request=");
How do I get the username, password, request, and option without having use the Regex.Split method?
Upvotes: 0
Views: 469
Reputation: 2714
Several people have suggested you can access the querystring via the Request object's QueryString indexer. That indexer appears in System.Web.HttpRequest, and the object in question is System.Net.HttpWebRequest. They are very different things. The HttpUtility class will parse the parameters out of the querystring for you:
Uri uri = new Uri("http://bookReport/request.aspx?user=abc&password=password&request=1&option=yes");
var qs = HttpUtility.ParseQueryString(uri.Query);
Upvotes: 1
Reputation: 3681
You can access the query string variables using Request.QueryString function as below
string userName = Request.QueryString["user"];
string password = Request.QueryString["password"];
string request = Request.QueryString["request"];
string option = Request.QueryString["option"];
By looking at your code, just got another approach for retrieving the query string values from a string. You can use the following code
Uri uri = new Uri(url);
var values= HttpUtility.ParseQueryString(uri.Query);
string userName = values["user"];
string password = values["password"];
string request = values["request"];
string option = values["option"];
Upvotes: 1
Reputation: 468
string userName = request.QueryString["user"];
string password = request.QueryString["password"];
Upvotes: 1