接收到远程服务器返回错误

接收到远程服务器返回错误

本文介绍了接收到远程服务器返回错误:(403)禁止消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到远程服务器返回错误:(403)禁止低于code块上的错误消息。特别是此行失败: VAR响应=(HttpWebResponse)(request.GetResponse());

在code完美的作品我开发的机器上,而不是在生产。我在两个环境中使用IIS 7.5。两款机器都漂洗补丁(安装了所有的Windows更新)。使用4.0 .NET框架。不知道为什么它的工作原理一体机而不是其他其他的。

code:

  VAR URI =新的URI(URL);
VAR请求=(HttpWebRequest的)WebRequest.Create(URI);request.KeepAlive = FALSE;
request.ProtocolVersion = HttpVersion.Version10;
request.Timeout = System.Threading.Timeout.Infinite;
request.Method = @POST;
request.ContentType = @应用程序/ x-WWW的形式urlen codeD;
request.ContentLength = postData.Length;
request.UserAgent = @的Mozilla / 4.0(兼容; MSIE 6.0; Windows NT的5.0; .NET CLR 1.1.4322);
request.Accept =* / *;//设置流变量并执行发送/数据检索
流writeStream = request.GetRequestStream();
VAR字节= System.Text.Encoding.ASCII.GetBytes(POSTDATA);
writeStream.Write(字节,0,bytes.Length);
writeStream.Close();变种响应=(HttpWebResponse)(request.GetResponse());
变种responseStream = response.GetResponseStream();
VAR readStream =新的StreamReader(responseStream,Encoding.UTF8);
pageResponse = readStream.ReadToEnd();


解决方案

您可能需要指定凭据

  request.Credentials = CredentialCache.DefaultCredentials;

或你,如果你需要通过特定的凭证传递:

  request.Credentials =新NetworkCredentials(用户,密码);

了解

I am receiving "The remote server returned an error: (403) Forbidden" error message on the block of code below. Specifically this line is failing: var response = (HttpWebResponse)(request.GetResponse());

The code works perfectly on my dev machine, but not in production. I am using IIS 7.5 in both environments. Both machines are fulling patched (all Windows updates installed). Using 4.0 .Net Framework. Not sure why it works on one machine and not other other.

Code:

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();
解决方案

You might need to specify credentials

request.Credentials = CredentialCache.DefaultCredentials;

or you if you need to pass specific credentials pass:

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

More information about NetworkdCredential Class

这篇关于接收到远程服务器返回错误:(403)禁止消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:56