C# HttpWebRequest。远程服务器返回错误:(500)内部服务器错误
我需要C#中的HttpWebRequest帮助。下面几行代码对本地IIS运行良好,但当我上传到远程服务器时,它开始告诉我“远程服务器返回了一个错误:(500)内部服务器错误”。我尝试了许多不同的GET和POST方法,但无法找出问题所在。请看一下下面的代码,让我知道这有什么问题C# HttpWebRequest。远程服务器返回错误:(500)内部服务器错误,c#,httpwebrequest,internal-server-error,C#,Httpwebrequest,Internal Server Error,我需要C#中的HttpWebRequest帮助。下面几行代码对本地IIS运行良好,但当我上传到远程服务器时,它开始告诉我“远程服务器返回了一个错误:(500)内部服务器错误”。我尝试了许多不同的GET和POST方法,但无法找出问题所在。请看一下下面的代码,让我知道这有什么问题 try { string postData = "applicaitonid=abc&deviceid=xyz"; string uri = System.Configuration.Configu
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方法,但它没有解决问题 我要在黑暗中冒险 是否可能是您的查询字符串中存在拼写错误,并且在代码中引用键时返回nullvalue异常
{ String postData = "applicaitonid=abc&deviceid=xyz";
}
应该是
{ String postData = "applicationid=abc&deviceid=xyz"; }
将此代码用于catch
catch (WebException e)
{
string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
return pageContent;
}
它将显示您面临的确切错误。您可以使用try-and-catch块查找根本原因
catch (WebException ex)
{
string message = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
}
我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。问题出在服务器端。您是否查看了异常中的响应主体?请参阅。忽略查询字符串中的拼写错误。这是打字错误。代码在本地运行良好。但是当我将它发布到远程服务器时,它给出了“远程服务器返回了一个错误:(500)内部服务器错误”。有很多“变量”,您的uri值如何?远程服务器上是否存在abapath目录?@Wombelite我确实遇到了同样的问题。我使用的是“userId”,而不是
userId
。谢谢,谢谢!这帮助我发现我的控制器上的[ValidateAntiyForgeryToken]
拒绝了我的错误请求。