gpt4 book ai didi

c# - HttpWeb 请求。远程服务器返回错误 : (500) Internal Server Error

转载 作者:太空狗 更新时间:2023-10-30 00:19:28 24 4
gpt4 key购买 nike

我需要有关 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 方法,但它并没有解决问题。

最佳答案

使用此代码捕获

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

它将显示您所面临的确切错误。

关于c# - HttpWeb 请求。远程服务器返回错误 : (500) Internal Server Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059237/

24 4 0
文章推荐: c# - "The given path' 不支持 s 格式”尝试根据当前时间创建文件时
文章推荐: python - Selenium Python 如何从
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com