【发布时间】:2013-11-18 22:24:46
【问题描述】:
我需要有关 C# 中 HttpWebRequest 的帮助。下面的代码行对于本地 IIS 工作正常,但是当我上传到远程服务器时,它开始给我“远程服务器返回错误:(500)内部服务器错误。”。我尝试了 GET 和 POST 方法的许多变体,但无法弄清楚问题所在。请查看下面的代码,让我知道这有什么问题。
try
{
string postData = "applicaitonid=abc&deviceid=xyz";
string uri = System.Configuration.ConfigurationManager.AppSettings.Get("baseUrl") + System.Configuration.ConfigurationManager.AppSettings.Get("ABApiPath") + "ConfirmAppBinding/?" + postData;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST"; // Set type Post
//request.Method = "GET";
request.UserAgent = Request.UserAgent.ToString();
request.ContentType = @"application/json";
request.MediaType = "application/json";
request.Accept = "application/json";
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version11;
//byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(postData);
request.Timeout = 500000; //Increase timeout for testing
Stream reqstr = request.GetRequestStream();
//reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
// Read Response
var httpResponse = request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
JsonMessage.message = streamReader.ReadToEnd();
streamReader.Close();
}
}
catch (WebException e)
{
JsonMessage.message = e.Message;
return Json(JsonMessage, JsonRequestBehavior.AllowGet);
}
正如我告诉你的,我使用了默认的 GET 方法,但它并没有解决问题。
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
问题出在服务器端。
-
您查看异常中的响应正文了吗?见stackoverflow.com/questions/11828843/…。
标签: c# httpwebrequest internal-server-error