调用HTTPS web服务方法

本文关键字:方法 服务 web HTTPS 调用 | 更新日期: 2023-09-27 18:18:44

我添加了一个web服务的web引用, url以https开头。并尝试调用以下c#代码

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;

我得到以下错误

The request failed with HTTP status 401: Unauthorized.

我猜原因是"BasicHttpSecurityMode尚未设置为传输"(这是可能的,如果我添加作为服务参考)。我找不到任何方法来设置它。或者这是不可能实现的?这是一个VS.Net 2005项目,

更新:我还添加了以下内容,但仍然有相同的错误

webservice.UseDefaultCredentials = true
var response = webservice.GetMethods();

working version:以下代码开始工作。感谢@user469104

testwebservices.BasicHttpBinding_GDataService webservice = new testwebservices.BasicHttpBinding_GDataService ();
webservice.Credentials = new NetworkCredential("username", "password");
var response = webservice.GetMethods();

调用HTTPS web服务方法

您得到的错误与https本身无关。401表示任何凭据(如果有的话)都没有被授权访问服务。

我猜这是由IIS托管的web服务?如果是这样,并且您可以控制它,请验证"集成身份验证"是否启用(我假设这是您正在尝试使用的,因为您指定了"协商"),并确保您正在传递有效的凭据。正如DJ KRAZE在他的评论中所说,如果你试图传递域凭据,请确保你也在传递域。