一尘不染

HttpWebRequest没有传递凭据

c#

我试图用来HTTPWebRequest访问REST服务,并且在传递凭据时遇到问题,请参见下面的代码。我读过,NetworkCredential它不支持SSL,而且我正在访问一个HTTPS网站。有谁知道类似的类NetworkCredential支持SSL?

Uri requestUri = null;
Uri.TryCreate("https://mywebserver/webpage", UriKind.Absolute, out requestUri);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
NetworkCredential nc = new NetworkCredential("user", "password");
request.Credentials = nc;
request.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

阅读 308

收藏
2020-05-19

共1个答案

一尘不染

如果您的服务器使用NTLM身份验证,则可以尝试以下操作:

CredentialCache cc = new CredentialCache();
cc.Add(
    new Uri("https://mywebserver/webpage"), 
    "NTLM", 
    new NetworkCredential("user", "password"));
request.Credentials = cc;
2020-05-19