【问题标题】:web response always returns error 500Web 响应总是返回错误 500
【发布时间】:2017-09-25 08:05:35
【问题描述】:

我正在尝试做一个小应用程序来测试使用 .net 框架的 Web 服务。 我总是从服务器收到错误 500。

这是我的代码 首先是服务器端代码

public class BalanceInquiry : System.Web.Services.WebService
{
    [WebMethod]
    public string getAccount(string accNbr)
    {
        return accNbr;
    }
}

这是我的客户端代码:

 class Program
{
    static void Main(string[] args)  
    {  
        //creating object of program class to access methods  
        Program obj = new Program();  
        Console.WriteLine("Please Enter Input values..");  
        //Reading input values from console  
        string a =(Console.ReadLine());  
       // int b = Convert.ToInt32(Console.ReadLine());  
        //Calling InvokeService method  
        obj.InvokeService(a); 

    }  
    public void InvokeService(string a )  
    {  
        //Calling CreateSOAPWebRequest method  
        HttpWebRequest request = CreateSOAPWebRequest();  

        XmlDocument SOAPReqBody = new XmlDocument();  
        //SOAP Body Request  
        SOAPReqBody.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>  
        <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
         <soap:Body>  
            <getAccount xmlns=""http://tempuri.org/"">  
              <accNbr>" + a + @"</accNbr>  
            </getAccount>  
          </soap:Body>  
        </soap:Envelope>");  


        using (Stream stream = request.GetRequestStream())  
        {
            using (StreamWriter stmw = new StreamWriter(stream))
            {
                stmw.Write(SOAPReqBody);
            }
        }

        try
        {
            //Geting response from request  
            WebResponse response = request.GetResponse();
        }

        catch (WebException e)
        {
            Console.WriteLine(e.ToString());

        }
        Console.Read();

    }  

    public HttpWebRequest CreateSOAPWebRequest()  
    {  
        //Making Web Request  
        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http://localhost:62325/BalanceInquiry.asmx");  
        //SOAPAction  
        Req.Headers.Add("SOAPAction:http://tempuri.org/");  
        //Content_type  
        Req.ContentType = "text/xml;charset=\"utf-8\"";  
        Req.Accept = "text/xml";  
        //HTTP method  
        Req.Method = "POST";  
        //return HttpWebRequest  
        return Req;  
    }  
}  

this is the output

错误是

System.Net.WebException:远程服务器返回错误:(500)内部服务器错误。 在 System.Net.HttpWebRequest.GetResponse() 在 C:\Users\msaliba\Documents\Visual Studio 2010\Projects\consumeWebSer\consumeWebSer\Program.cs:line 53 中的 consumeWebSer.Program.InvokeService(String a) 处

有没有人知道什么会导致这个错误。它是我的代码中的东西还是可能是其他东西?

【问题讨论】:

  • 至于 every 错误 500:检查服务器上的错误详细信息。
  • 请将这些错误消息作为文本而不是图像发布,因为这样更容易搜索和复制粘贴。
  • SOAPAction 设置为Addition,实际的SOAP 主体有HelloWorld 作为参数,而服务器方法是getAccount。只要三者不匹配,这将永远无法工作。
  • 对不起,我会改变它..但问题仍然存在
  • 如何查看服务器上的详细信息?

标签: c# .net web-services asp.net-web-api asmx


【解决方案1】:

如果我理解正确,您希望将 SOAP 与 WebApi 一起使用,这更适合 REST 实现;如果需要 SOAP,通常您会希望使用 WCF。

无论如何,您的客户端正在发送一个 xml,但我没有看到您的服务解析它以提取 accNbr 参数的部分,也没有在上述 xml 中看到 accNbr。

另外,我不确定您的 getAccount 方法是否公开为 Post 方法。

【讨论】:

  • 我没有发布整个代码,因为它从未通过网络响应。我正在做一个小测试,我有一个更大的项目需要使用肥皂。
  • 是的,当然它不会在客户端传递 Web 响应:错误发生在服务器上,您的 Web 服务应该解析 xml。您的 Web 服务是否解析 xml?您可以在“getAccount”中添加一些代码吗?另外,我看不到您从客户端调用“getAccount”的位置:也许您应该将其添加到“CreateSOAPWebRequest”中的地址,如“localhost:62325/BalanceInquiry.asmx/getAccount”。此外,如果您使用 webapi 并公开 Post 方法,则应将其命名为例如PostGetAccount 使其工作,即使用“Post”后缀。
  • 我不必解析服务器中的 xml。好吧,我在 xml 部分中调用它
  • 我认为您期望 webapi(即 Rest)为您进行 xml 解析。你?我不认为它是开箱即用的,但你必须自己做...... Wcf 会自动完成,但你没有使用它,对吧?
  • 好吧,我有点困惑。我真的不知道我该怎么办。我想创建一个 Web 服务并在客户端中使用它。
猜你喜欢
  • 1970-01-01
  • 2016-05-19
  • 2020-10-28
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
相关资源
最近更新 更多