2017-09-25 72 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