您的位置:首页 > 理论基础 > 计算机网络

HttpWebResponse远程服务器返回错误: (500) 内部服务器错误 的解决办法

2010-02-26 18:09 841 查看
例子:

 

myHttpWebRequest.Method = "POST";
Console.WriteLine ("/nPlease enter the data to be posted to the (http://www.contoso.com/codesnippets/next.asp) Uri :");

// Create a new string object to POST data to the Url.
string inputData = Console.ReadLine ();

string postData = "firstone=" + inputData;
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (postData);

// Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

// Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length;

Stream newStream = myHttpWebRequest.GetRequestStream ();

newStream.Write (byte1, 0, byte1.Length);
Console.WriteLine ("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength);

// Close the Stream object.
newStream.Close ();

 

一般都是执行到 Stream newStream = myHttpWebRequest.GetRequestStream (); 的时候会抛出500 服务器错误,而且有时候会,有时候不会,本人试了很久才找到解决办法,

 

  原来是 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; 这个的原因,改成

myHttpWebRequest.ContentType = "text/html"; 问题就解决了!汗!

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐