Home All Groups Group Topic Archive Search About

Uri with "@" in username

Author
8 Nov 2005 4:31 PM
Hans
Hello,

I am trying to create a Webrequest. The used Uri is something like
"http://usern***@domain.com:passw***@www.shfgshfge.com".
Note that the username contains an "@"-character.
I tried two ways. Neither does work.

1. Directly creating an Uri:

    Uri u=new Uri("http://usern***@domain.com:passw***@www.shfgshfge.com");

    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="usern***@domain.com";
    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

Author
8 Nov 2005 4:58 PM
JS
Hans...

Use the NetworkCredential class.  Set the UserName and Password
properties to the values you specified.  Then take your WebRequest and
set the Credentials property to the NetworkCredential object you has
instantiated.

The UserName and Password properties are strings so there should be no
issues with the ampersands.

J.


Hans wrote:
Show quote
> Hello,
>
> I am trying to create a Webrequest. The used Uri is something like
> "http://usern***@domain.com:passw***@www.shfgshfge.com".
> Note that the username contains an "@"-character.
> I tried two ways. Neither does work.
>
> 1. Directly creating an Uri:
>
>     Uri u=new Uri("http://usern***@domain.com:passw***@www.shfgshfge.com");
>
>     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="usern***@domain.com";
>     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

AddThis Social Bookmark Button