HTTPWebRequest返回401未经授权

时间:2018-10-12 05:30:57

标签: c# .net httpwebrequest http-status-code-401

我向URL发出POST HTTPWebRequest以下载文件。问题是请求失败,消息身份验证失败。但是通过POSTMAN应用程序发出的相同请求也可以正常工作。我收到的错误是: 远程服务器返回错误:(401)未经授权。协议错误。

两个请求之间的小提琴手捕获表明POSTMAN几乎没有其他密码,ec_point_formats,elliptic_curves和signature_algs。不确定是否重要,但是为了使这篇文章简短,我没有给出实际差异,但可以根据要求提供。

我使用的示例代码:

// create a request 
HttpWebRequest request; = (HttpWebRequest)WebRequest.Create(inputUri);

SetProxy(inputProxyUri, inputProxyUser, inputProxyPassword, request);

request.ProtocolVersion = HttpVersion.Version11;

//Set authorization 
string authorisation = string.Format("{0}:{1}", user, pass);
string encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(authorisation));
string header = string.Format("{0} {1}", "Basic", encoded);
request.Headers[HttpRequestHeader.Authorization] = header;

request.KeepAlive = false;
request.Method = "POST";

byte[] postBytes = Encoding.ASCII.GetBytes(requestParams);
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();

//Get response stream
System.IO.Stream responseStream = ((HttpWebResponse)request.GetResponse()).GetResponseStream();

我玩过下面提到的请求对象:

request.ProtocolVersion = HttpVersion.Version11;
   request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;

   request.UseDefaultCredentials = true;
   request.PreAuthenticate = true;
   request.Credentials = CredentialCache.DefaultCredentials;

   request.Accept = "*/*";

还更改了注册表以启用TLS 1.2,如https://www.derekseaman.com/2010/06/enable-tls-12-aes-256-and-sha-256-in.html中所述,为客户端和服务器SCHANNEL通信启用TLS-1.2,但运气不佳。

任何帮助将不胜感激。

0 个答案:

没有答案