如何避免错误异常。 (500)从ASP.NET中的托管Web服务获取响应数据? [英] How to avoid exception of error. (500) to fetch the response data from the hosted web service in ASP.NET?

查看:70
本文介绍了如何避免错误异常。 (500)从ASP.NET中的托管Web服务获取响应数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到异常远程服务器返回错误。 (500)尝试使用c#从asp.net中的托管Web服务获取响应数据时出现内部服务器错误。托管Web服务也可以访问。



Request API采用json格式,如下所示:

I am getting exception of The remote server returned an error. (500) Internal Server Error on trying to fetch the response data from the hosted Web Service in asp.net with c#. Hosted Web Service is accessible too.

The Request API is in json format as below:

<string xmlns="http://tempuri.org/">
{"Req_UserId":"Samuel.Jacob","Req_Channel_ID":"IB","Req_Reference_No":"IB120219100103"}
</string>





响应API内容如下:





Response API content is as below:

{
   "AuthenticateSSOResponse":
 {
       "Reference_No": "IB120219100103", 
       "Token_No":  "120219100103256"
   }
}





我的尝试:



代码如下:



What I have tried:

Code as below:

public string CallWebService()
         {

            string URL = ConfigurationManager.AppSettings["NeST_WSI_API"] != null ? ConfigurationManager.AppSettings["NeST_WSI_API"].ToString() : string.Empty;



          JavaScriptSerializer oSerializer = new JavaScriptSerializer();
          string ReqJSON = BuildReqJson();
          System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
          request.Method = "POST";
          request.Accept = @"application/json";
          request.ContentType = @"application/json";
          Byte[] byteArray = encoding.GetBytes(ReqJSON);
          request.ContentLength = byteArray.Length;



          using (Stream stm = request.GetRequestStream())
          {
              stm.Write(byteArray, 0, byteArray.Length);

          }



          using (StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream()))
          {

              //string jsonResponse = responseReader.ReadToEnd();
              string jsonResponse = "jsonResponse";

              if (jsonResponse != null)
              {


              }
          }

          return "";


      }
      #endregion

推荐答案

我怀疑是500状态代码表示服务器端存在问题,但不一定与应用程序本身有关,但与其接收的有效负载有关。某些REST服务(使用JSON或XML)使用库反序列化内容。如果传递给请求的数据与该端点的预期数据不匹配,则REST服务可能会抛出500,因为它无法正确反序列化有效负载。



如果您确信有效负载是正确的,我建议您查看服务器端的日志文件以确定可能导致问题的原因(如果反序列化有问题,通常会记录Web服务)。如果您无法访问此项,那么可能会使用REST服务支持提出一张票?



在执行任何此操作之前,您是否尝试过运行请求像 Postman 这样的工具[ ^ ],它可以让你立即查看请求+响应主体?也许500会在响应中返回其他信息。
I suspect a 500 status code would indicate a problem on the server-side, but not necessarily with the application itself but with the payload it's receiving. Some REST services (which consume JSON or XML) deserialize the content using libraries. If the data being passed into the request doesn't match with what is expected for that end-point, the REST service might throw a 500 because it could not correctly deserialize the payload.

If you're absolutely confident that the payload is correct, I'd recommend looking at log files at the server-side to determine what might be causing the problem (usually web-services will log if there's problems deserializing). If you don't have access to this then perhaps raise a ticket with the REST service support?

Before you do any of this, have you tried running your request through a tool like Postman[^] which lets you immediately view the request + response bodies? Perhaps the 500 is being returned with additional information in the response itself.


这篇关于如何避免错误异常。 (500)从ASP.NET中的托管Web服务获取响应数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆