gpt4 book ai didi

c# - 接收远程服务器返回错误 : (403) Forbidden message

转载 作者:太空狗 更新时间:2023-10-29 23:46:52 26 4
gpt4 key购买 nike

我在下面的代码块中收到“远程服务器返回错误:(403) 禁止访问”错误消息。具体来说,这一行失败了:var response = (HttpWebResponse)(request.GetResponse());

该代码在我的开发机器上运行良好,但在生产环境中运行不佳。我在这两种环境中都使用 IIS 7.5。两台机器都打好了补丁(安装了所有 Windows 更新)。使用 4.0 .Net 框架。不确定为什么它在一台机器上工作而不在其他机器上工作。

代码:

var uri = new Uri(url);
var request = (HttpWebRequest)WebRequest.Create(uri);

request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Timeout = System.Threading.Timeout.Infinite;
request.Method = @"POST";
request.ContentType = @"application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
request.Accept = "*/*";

//setup the stream variables and do the send/retrieve of data
Stream writeStream = request.GetRequestStream();
var bytes = System.Text.Encoding.ASCII.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
writeStream.Close();

var response = (HttpWebResponse)(request.GetResponse());
var responseStream = response.GetResponseStream();
var readStream = new StreamReader(responseStream, Encoding.UTF8);
pageResponse = readStream.ReadToEnd();

最佳答案

您可能需要指定凭据

request.Credentials = CredentialCache.DefaultCredentials;

如果您需要传递特定的凭据,或者您传递:

request.Credentials = new NetworkCredentials("user", "password");

关于 NetworkdCredential Class 的更多信息

关于c# - 接收远程服务器返回错误 : (403) Forbidden message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14184199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com