使用 SSL 在 C# 中调用 webservice - '(401) Unauthorized'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/798266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling webservice in C# using SSL - '(401) Unauthorized'
提问by Chris B
I'm calling a method on a web service from behind a proxy server using the following code:
我正在使用以下代码从代理服务器后面调用 Web 服务上的方法:
myWebService.TestWebService webservice = new myWebService.TestWebService();
webservice.Url = "http://test.com/webservice?wsdl";
WebProxy proxy = new WebProxy("1.2.3.4", 8080);
proxy.Credentials = new NetworkCredential("username", "password");
webservice.Proxy = proxy;
string response = webservice.TestWebMethod();
This works fine when using HTTP, I get the response I'm expecting in the 'response' string. However - if I change the URL to HTTPSthen I get a (401) Unauthorizedresponse.
这在使用 HTTP 时工作正常,我在“响应”字符串中得到了我期望的响应。但是 - 如果我将 URL 更改为HTTPS,则会收到(401) 未经授权的响应。
If I put the URL into my browser it works fine using HTTP or HTTPS.
如果我将 URL 放入我的浏览器,它可以使用 HTTP 或 HTTPS 正常工作。
I've added code to handle the SSL certificate validation by creating a System.Net.ServicePointManager.ServerCertificateValidationCallback
delegate but the code never gets this far. The request is rejected before it validates the certificate or so it seems.
我通过创建System.Net.ServicePointManager.ServerCertificateValidationCallback
委托添加了代码来处理 SSL 证书验证,但代码从来没有走这么远。该请求在验证证书之前被拒绝,或者看起来如此。
Any help is really appreciated...
任何帮助真的很感激......
采纳答案by stevehipwell
Do you need credentials to navigate to the SSL url?
If so you need the web service credentials set.
您是否需要凭据才能导航到 SSL url?
如果是这样,您需要设置 Web 服务凭据。
Have you tried adding a web reference in Visual Studio using the SSL url?
If you can't add web reference through Visual Studio then the code is not going to work either.
您是否尝试过使用 SSL url 在 Visual Studio 中添加 Web 引用?
如果您无法通过 Visual Studio 添加 Web 引用,则代码也将无法工作。
Can you make sure that the last thing that you set is the proxy (e.g. change the url before you set the proxy)?
There is a small chance that the proxy could be lost, this should be the very last thing to try
你能确定你设置的最后一件事是代理吗(例如,在设置代理之前更改 url)?
代理丢失的可能性很小,这应该是最后尝试的事情
回答by Rick Hochstetler
Here is an example using a client cert (which i'm sure you don't need) but might provide some insight & using credentials to a web service.
这是一个使用客户端证书的示例(我确定您不需要),但可能会提供一些见解并使用 Web 服务的凭据。
WebService.ManageOutboundDelivery oWS = new WebService.ManageOutboundDelivery();
if (My.Settings.HasClientCert == true) {
X509Certificate2 signedCert = new X509Certificate2(HttpContext.Current.Server.MapPath(My.Settings.ClientCertName), My.Settings.ClientCertPW);
oWS.ClientCertificates.Add(signedCert);
}
System.Net.CredentialCache oCred = new System.Net.CredentialCache();
Net.NetworkCredential netCred = new Net.NetworkCredential(My.Settings.WebServiceUID, My.Settings.WebServicePW);
oCred.Add(new Uri(oWS.Url), "Basic", netCred);
oWS.Credentials = oCred;
Have you also checked the SSL cert is valid - i'm guessing you would see this when you hit it through the browser but it could be causing a problem trying to hit it programmatically.
您是否还检查了 SSL 证书是否有效 - 我猜当您通过浏览器点击它时您会看到这一点,但它可能会导致尝试以编程方式点击它的问题。