2017-09-25 66 views
-2

我想做一个小应用程序来测试使用.net框架的Web服务。 我总是从服务器得到错误500。网站响应总是返回错误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: The remote server returned an error: (500) Internal Server Error. at System.Net.HttpWebRequest.GetResponse() at consumeWebSer.Program.InvokeService(String a) in C:\Users\msaliba\Documents\Visual Studio 2010\Projects\consumeWebSer\consumeWebSer\Program.cs:line 53

有没有人有任何想法可以导致此错误。这是我的代码中的东西,还是可能是别的?

+1

至于_every_ error 500 ever:检查服务器上的错误详细信息。 –

+0

请将这些错误消息作为文本发布,而不是图片,因为它使搜索和复制粘贴更容易。 –

+1

SOAPAction设置为'Addition',实际的SOAP主体具有'HelloWorld'作为参数,但服务器方法是'getAccount'。只要三个不匹配,这将永远不会工作。 –

回答

0

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

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

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

+0

我没有发布整个代码,因为它从来没有通过网络响应。 我正在做一个小测试,我有一个更大的项目,我需要使用肥皂。 – michel

+0

是的,当然它不会在客户端上传递Web响应:错误发生在服务器上,您的Web服务应该解析XML。你的web服务是否解析xml?你可以在'getAccount'中加入一些代码吗? 另外,我没有看到你从你的客户端调用'getAccount':或许你应该将它添加到'CreateSOAPWebRequest'中的地址,如'http:// localhost:62325/BalanceInquiry.asmx/getAccount' 。 此外,如果您使用webapi并且公开Post方法,则应将其命名为例如PostGetAccount使其工作,即使用“Post”后缀。 –

+0

我不必解析服务器中的xml。以及我在xml部分调用它 – michel