Uri with "@" in username

H

Hans

Hello,

I am trying to create a Webrequest. The used Uri is something like
"http://[email protected]:[email protected]".
Note that the username contains an "@"-character.
I tried two ways. Neither does work.

1. Directly creating an Uri:

Uri u=new Uri("http://[email protected]:[email protected]");

It fails with an System.UriFormatException (Something like "The hostname
cannot be parsed.")


2. Using an UriBuilder:

UriBuilder ub=new UriBuilder();
ub.Host="www.shfgshfge.com";
ub.Scheme="http";
ub.Path="/";
ub.Username="(e-mail address removed)";
ub.Password="password";

Uri u=ub.Uri;

It fails with the above error.


Unfortunately, the UserInfo property of Uri is readonly.
How can I generate a valid Uri from this string and subsequently use it in a
WebRequest?

TIA
 
P

Peter Rilling

Don't know if this will work, but another alternative might be to encode the
@ symbol since it is a special character in the URL. Find out what the code
should be (something like %xx). Also, you might create a dummy webpage that
has a hyperlink with that URL formatted as you want and see how IE sends the
request.
 
N

Nicholas Paldino [.NET/C# MVP]

Actually, this is exactly how you would do it. My password in some
places had a @ character in it, so I had to learn the hard way =)
 
J

John Timney \( MVP \)

Pretty sure you cant pass that form on the url line - if you dig into the
URI spec the syntax is:

[ user [ : password ] @ ] hostport as defined in the wc3 spec for URI's
http://www.w3.org/Addressing/URL/uri-spec.html

The use of the @ is permitted, but only before the host, or within a nntp or
mail address format as URI as it is a reserved character, as defined in
rfc2396
http://www.ietf.org/rfc/rfc2396.txt, so its only going to be accepted where
the RFC permits it to be, and that doesn't appear to be in the username.

Out of interest, does the path work if you type it into a browser?

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top