问题描述
我有一个问题,我似乎无法解决问题。我正在尝试使用 httpClient 调用我的 web api,但收到 500 内部服务器错误。搜索了几个帖子,但找不到确切的问题。我知道它是 JSON 格式,但不确定缺少什么。以下是我的客户端功能:
[Microsoft.sqlServer.Server.sqlProcedure(Name = "myCallStoredProc")]
public static void myCallStoredProc(sqlString BaseApiUrl,sqlString ApiName,sqlString ApiKey,sqlString InputJson,out int RtnCd,out string RtnMsg)
{
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(BaseApiUrl.ToString());
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("API_KEY",ApiKey.ToString());
//var content = new StringContent(null,Encoding.UTF8,"application/json");
var content = new StringContent(InputJson.ToString(),"application/json");
var response = httpClient.PostAsync(ApiName.ToString(),content).Result;
RtnCd = response.IsSuccessstatusCode == true ? 0 : -1;
RtnMsg = RtnCd == -1 ? response.Content.ReadAsstringAsync().Result : "";
return;
}
catch (Exception ex)
{
RtnCd = -1;
RtnMsg = ex.ToString();
}
}
这是我的 InputJson:
"{\"PnsHandle\":\"<value>\",\"PnsType\":\"<value>\",\"Template\":\"{\"data\":{\"title\":\"$(title)\",\"message\":\"$(message)\"}}\",\"TagList\":\"<value>\",\"InstallationId\":\"<value>\",\"AzureHubServiceName\":\"<value>\",\"AzureHubName\":\"<value>\",\"AzureHubListenAccessKeyName\":\"<value>\",\"AzureHubListenAccesskeyvalue\":\"<value>\"}"
当我使用必需的输入和高于 InputJson 值执行 myCallStoredProc 时,出现以下错误:
解析值后遇到意外字符:d。路径“模板”,第 1 行,位置 110。
我的 web api 如下所示:
[HttpPost]
public async Task<HttpResponseMessage> myAPI(HttpRequestMessage request)
{
try
{
string input = await request.Content.ReadAsstringAsync();
JObject inputValues = JObject.Parse(input);
// Do Something with inputValues
return Request.CreateResponse(HttpStatusCode.OK,result);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError,ex);
}
}
我尝试使用在线 json 格式化程序格式化输入 json,在删除转义符 '' 后,json 结果很好。
我搜索了多个具有相同问题 here 和 here 的帖子,但他们正在讨论如何发送帖子数据,例如使用 httpwebrequest 或使用我已经在做的 httpClient。
对我在这里可能做错的事情有任何评论吗?在我看来是 json 格式问题,但不确定。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)