我正在尝试通过具有基本身份验证的 https url 进行网络请求。它不工作!下面是我的代码,如果我使用非安全 URL 与安全 URL,它实际上可以工作,而且我无法弄清楚我做错了什么。只能在不安全的情况下找到,但是当使用安全 url 时,我收到 401 用户身份验证错误。可能是有人在服务器上设置错误,还是我的代码?
有人可以帮助我吗?
var req = System.Net.HttpWebRequest.Create(Url) as HttpWebRequest;
req.Method = Method.ToString();
req.ContentType = "application/json";
req.Date = RequestTime;
req.Proxy = null;
string credentials = String.Format("{0}:{1}", "xxxx", "xxxx");
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
req.Headers.Add("Authorization", authorization);
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string responsebody = readStream.ReadToEnd();
Console.WriteLine(responsebody);
response.Close();
readStream.Close();