Tomas
Tomas

Reputation: 18097

Request.QueryString usage to read passed url

I use code Request.QueryString["u"] to read passed URL to my web application. Everything works fine if passed url do not has parameters but for example if such url is submitted

http://mywebapp:80/submit.aspx?u=http://www.submitedurl.com/top_sellers_pdf.php?GoodThru=7-21-2011&comments=This+is+a+test+for+PDF

the Request.QueryString["u"] return

http://www.submitedurl.com/top_sellers_pdf.php?GoodThru=7-21-2011

and comments=This+is+a+test+for+PDF is ignored.

I understand why this happens no need to explain :), but how this could be solved?

One solution I think would be to surround parameter with quotes. Like this

http://mywebapp:80/submit.aspx?u="http://www.submitedurl.com/top_sellers_pdf.php?GoodThru=7-21-2011&comments=This+is+a+test+for+PDF"

What other solution could be?

Upvotes: 1

Views: 828

Answers (4)

Karel
Karel

Reputation: 2212

You can use the raw url and parse it yourself or you can loop through the Request.Querystring NameValueCollection:`

foreach (KeyValuePair<string, string> item in Request.QueryString)
    {
       string  a = item.Key; 
        string b = item.Value;

    }

You'll know the U stands for the original base url, the other items are the params in the original url

Upvotes: 0

Jez
Jez

Reputation: 29973

Whatever is passing that URL to your application is doing it wrong. RFC 2396 states that & is a reserved character in URIs, and must be escaped to %26 if it is to be treated as data, and not a query string separator. Is it possible to fix the behaviour of whatever is passing the URLs to your web application?

Failing that, you'll need to grab the query string and parse it yourself. You can get the whole query string as one string using:

Request.QueryString.ToString()

... and get everything after the 'u=' argument using:

Request.QueryString.ToString().Substring(Request.QueryString.ToString().IndexOf("u=")+2)

Upvotes: 0

Mantorok
Mantorok

Reputation: 5266

When you build your querystring, use:

var value = HttpUtility.UrlEncode("My querystring with & inside it");

Upvotes: 1

Schroedingers Cat
Schroedingers Cat

Reputation: 3129

If you are in control of the URL parameter, you can replace the & with something else when the url is generated, and replace it back when you extract it.

Upvotes: 0

Related Questions