我在下面的代码块上收到“远程服务器返回错误:(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();