Hi everybody,
I need some help doing a rfc 2617 basic HTTP authentication to call a web service in C#. I've been trying in many ways and I'm really stuck. Here's an example of my current code
string url = "http://somedomain.com/somegenericservice";
WebRequest myReq = WebRequest.Create(url);
string username="****";
string password="****";
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
MessageBox.Show(content);
WebService.Service WebService = new WebService.Service ();
Uri uri = new Uri(url);
System.Net.NetworkCredential NC = new System.Net.NetworkCredential("****", "****");
System.Net.ICredentials IC = NC.GetCredential(uri, "Basic");
WebService.Credentials = IC;
WebService.PreAuthenticate=true;
WebService.Servicecall("****");
With this code, I get a Http 500 error (internal error). If I just use the normal network credentials, I get a HTTP 401 error, not authorized.
This is freaking me out a bit. Can anybody give me a hand?
Thanks in advance.
BR