我需要消费需要基本的先发制人身份验证的Web服务。我有以下代码,但是在响应上遇到错误 -
'远程服务器返回错误:(403(禁止。'
用户凭据是正确的。有什么想法怎么了?
string url = "MYURL";
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
string user = "USER";
string pwd = "PASSWORD";
string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + pwd));
req.PreAuthenticate = true;
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Headers.Add("Authorization", auth);
WebResponse resp = req.GetResponse();
resp.Close();
req = HttpWebRequest.Create(url) as HttpWebRequest;
req.PreAuthenticate = true;
req.Credentials = new NetworkCredential(user, pwd);
resp.Close();
401
是当您无法进行身份验证时收到的错误代码(即,您是谁(。如果您获得了403
,则表示服务器知道您是谁,但仍然认为不应允许您访问。
我想您应该与为您提供证书的人交谈并询问他。