远程服务器返回错误: (401) 未经授权

2020-10-17 10:27发布

  string responseBody = "";
            HttpWebRequest request;
            try
            {
                var code = Encoding.UTF8;
                byte[] data = code.GetBytes(strPostdata);

                request = (HttpWebRequest)WebRequest.Create(new Uri(posturl));
                request.UseDefaultCredentials = true;
                request.PreAuthenticate = true;


                request.Credentials = CredentialCache.DefaultCredentials;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(data, 0, data.Length);
                    requestStream.Close();
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                responseBody = stream.ReadToEnd();
                stream.Close();
                response.Close();
            }
            catch (WebException ex)
            {
                responseBody = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
            }

在网上也找了几个post提交的方法 都有这个问题 但是使用http在线工具测试 没有这个问题

2条回答
The star\"
2楼-- · 2020-10-17 10:46

确定是这个网址有问题还是所有的网址用这种方法都有问题。两个情况解决方法不一样的。

查看更多
等我变得足够好
3楼-- · 2020-10-17 10:50

就是没有访问权限呗

 

查看更多
登录 后发表回答