I have added a web reference of a web service which url starts with https. And trying to call by the following C# code
testwebservices.BasicHttpBinding_GDataService webservice = new testwebservices.BasicHttpBinding_GDataService ();
Uri uri = new Uri(webservice.Url);
CredentialCache cache = new CredentialCache();
cache.Add(uri, "Negotiate", new NetworkCredential("username", "password", "domainName"));
webservice.Credentials = cache;
var response = webservice.GetMethods();
return response;
I am getting the following error
The request failed with HTTP status 401: Unauthorized.
I guess the reason is "BasicHttpSecurityMode has not been set as Transport" (which is possible if i add as service reference). I could not find any way to set that. Or this cannot be not achievable? This is a VS.Net 2005 project,
Updated: I have also add the following but still having the same error
webservice.UseDefaultCredentials = true
var response = webservice.GetMethods();
working version : The following code started to work. Thanks to @user469104
testwebservices.BasicHttpBinding_GDataService webservice = new testwebservices.BasicHttpBinding_GDataService ();
webservice.Credentials = new NetworkCredential("username", "password");
var response = webservice.GetMethods();
The error you are getting is not related to https per se. The 401 means that whatever credentials are making it through to the server, if any, are not authorized to access the service.
I am guessing this is a web service hosted by IIS? If so, and you have control over it, verify that 'Integrated Authentication' is enabled (I am assuming this is what you are trying to use since you are specifying 'negotiate') and make sure you are passing valid credentials. As DJ KRAZE said in his comment, if you are trying to pass domain credentials, make sure you are also passing the domain.