HttpWebRequest的.远程服务器返回错误:(500)内部服务器错误

sim*_*sim 4 c# httpwebrequest internal-server-error

我需要在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);
}
Run Code Online (Sandbox Code Playgroud)

正如我告诉过你的,我使用了默认的GET方法,但它没有解决问题.

San*_*ger 10

使用此代码进行捕获

catch (WebException e)
{
   string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
   return pageContent;
}
Run Code Online (Sandbox Code Playgroud)

它会显示您面临的确切错误.


Wom*_*ite 1

我会在黑暗中刺一剑

是否可能是您的查询字符串中存在拼写错误,并且当在代码中引用该键时,它会返回空值异常?

{ String postData = "applicaitonid=abc&deviceid=xyz"; 
}
Run Code Online (Sandbox Code Playgroud)

应该

{ String postData = "applicationid=abc&deviceid=xyz";  }
Run Code Online (Sandbox Code Playgroud)