WebClient fails to generate querystring containing pound sign (#)

G

Guest

When using the WebClient class, if you add a querystring parameter containing a pound sign ("#"), the WebClient class will fail to include the pound sign as well as everything else that should be included in the querystring following the pound sign. The following code illustrates the problem. Can anyone verify whether this is a bug in the Microsoft .NET Framework?

Dim client As New System.Net.WebClient
Dim q As System.Collections.Specialized.NameValueCollection = client.QueryString
q.Add("test1", "abc d.e")
q.Add("test2", "x#z")
q.Add("test3", "123")
client.UploadValues("http://localhost/a.aspx", q)

To verify, replace "localhost" above with a web server which logs incoming web hits including querystrings or with a web sever to which you can sniff TCP/IP packets.
 
T

Tian Min Huang

Hello,

Thanks for your post. As I understand, the problem you are facing is that
it fails to include the pound sign in the querystring. Please correct me if
there is any misunderstanding. I now share the following information with
you:

1. As you know, it illegal to use pound sign (#) in the URL like this. #
separates server processed part of URL from document anchor which is
client's part. You also encode it, but you can't pass it raw. Please refer
to the following specification:

RFC 2396 - Uniform Resource Identifiers (URI): Generic Syntax
http://www.faqs.org/rfcs/rfc2396.html

2. To work around, I suggest that you can use POST method as shown in the
following code snippet:

'--------client side------------
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
client.UploadValues("http://localhost/a.aspx", "POST", q)

'--------in aspx page, you can get the value the way like
Response.Write(Request.Form["test2"].ToString());

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Top