HttpWebRequest with authorization

J

Jochen Hahnen

Hi...

The following snippet of code works pretty good on the full .NET framework:

Code:

try
{
Uri url = new Uri(@"http://some.url/server/somebscwserver.cgi");
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://some.url/server/somebscwserver.cg
i");
NetworkCredential myCredentials = new NetworkCredential( "username",
"password");

CredentialCache myCredentialCache = new CredentialCache();
MyCredentialCache.Add(url, "Basic", myCredential);
req.Credentials = myCredentialCache;

System.IO.Stream response = req.GetResponse().GetResponseStream();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}


Unfortunately I need it to run on the .NET CompactFramework. And there the
class CredentialCache is not supported. Therefore I tried to get the same
behavior without using these class by simply leaving out these lines.

Code:

try
{
Uri url = new Uri(@"http://some.url/server/somebscwserver.cgi");
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://some.url/server/someBscwServer.cg
i");
NetworkCredential myCredentials = new NetworkCredential( "username",
"password");

// CredentialCache myCredentialCache = new CredentialCache();
// MyCredentialCache.Add(url, "Basic", myCredential);
// req.Credentials = myCredentialCache;
req.Credentials = myCredentials;

System.IO.Stream response = req.GetResponse().GetResponseStream();
// now throws an exeption
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}


But now the application throws the following execption: "The remote server
returned an error: (401) Unauthorized."

I already tried out to build up the req.Header myself but this also does not
work:

Code:

string base64EncodedAuthorizationString ="username" + ":" + "password";
byte[] binaryData = new Byte[base64EncodedAuthorizationString.Length];
binaryData = Encoding.UTF8.GetBytes(base64EncodedAuthorizationString);
base64EncodedAuthorizationString = Convert.ToBase64String(binaryData);
base64EncodedAuthorizationString = "Basic " +
base64EncodedAuthorizationString;
req.Headers.Set("Authorization", base64EncodedAuthorizationString);
req.PreAuthenticate = true;
req.Credentials = new NetworkCredential(txtUser.Text, txtPassword.Text);

-> (Same Error)


What am I doing wrong or what other possibilities do I have to authenticate
?

Thanks for your help,
Jochen Hahnen
 
E

Ed Kaim [MSFT]

I found this code I used to connect to the MapPoint Web service from a PPC.
It's not the same as making a pure Web connection, but it may serve as an
alternative.
net.mappoint.staging.FindServiceSoap fws = new
net.mappoint.staging.FindServiceSoap();

fws.Credentials = new System.Net.NetworkCredential("userName", "password",
"domain");
 
J

Jochen Hahnen

I could not find the class net.mappoint.staging.FindServiceSoap on my
framework.
It seems to me that this class is part of another sdk ...

Mfg,
Jochen

Ed Kaim said:
I found this code I used to connect to the MapPoint Web service from a PPC.
It's not the same as making a pure Web connection, but it may serve as an
alternative.
net.mappoint.staging.FindServiceSoap fws = new
net.mappoint.staging.FindServiceSoap();

fws.Credentials = new System.Net.NetworkCredential("userName", "password",
"domain");



Jochen Hahnen said:
Hi...

The following snippet of code works pretty good on the full .NET framework:

Code:

try
{
Uri url = new Uri(@"http://some.url/server/somebscwserver.cgi");
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://some.url/server/somebscwserver.cg
i");
NetworkCredential myCredentials = new NetworkCredential( "username",
"password");

CredentialCache myCredentialCache = new CredentialCache();
MyCredentialCache.Add(url, "Basic", myCredential);
req.Credentials = myCredentialCache;

System.IO.Stream response = req.GetResponse().GetResponseStream();
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}


Unfortunately I need it to run on the .NET CompactFramework. And there the
class CredentialCache is not supported. Therefore I tried to get the same
behavior without using these class by simply leaving out these lines.

Code:

try
{
Uri url = new Uri(@"http://some.url/server/somebscwserver.cgi");
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://some.url/server/someBscwServer.cg
i");
NetworkCredential myCredentials = new NetworkCredential( "username",
"password");

// CredentialCache myCredentialCache = new CredentialCache();
// MyCredentialCache.Add(url, "Basic", myCredential);
// req.Credentials = myCredentialCache;
req.Credentials = myCredentials;

System.IO.Stream response = req.GetResponse().GetResponseStream();
// now throws an exeption
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}


But now the application throws the following execption: "The remote server
returned an error: (401) Unauthorized."

I already tried out to build up the req.Header myself but this also does not
work:

Code:

string base64EncodedAuthorizationString ="username" + ":" + "password";
byte[] binaryData = new Byte[base64EncodedAuthorizationString.Length];
binaryData = Encoding.UTF8.GetBytes(base64EncodedAuthorizationString);
base64EncodedAuthorizationString = Convert.ToBase64String(binaryData);
base64EncodedAuthorizationString = "Basic " +
base64EncodedAuthorizationString;
req.Headers.Set("Authorization", base64EncodedAuthorizationString);
req.PreAuthenticate = true;
req.Credentials = new NetworkCredential(txtUser.Text, txtPassword.Text);

-> (Same Error)


What am I doing wrong or what other possibilities do I have to authenticate
?

Thanks for your help,
Jochen Hahnen
 

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