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
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