2017-09-25 16 views
-2

.net 프레임 워크를 사용하여 웹 서비스를 테스트하기 위해 작은 응용 프로그램을 만들려고합니다. 서버에서 항상 오류 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의 경우 : 서버의 오류 세부 사항을 검사하십시오. –

+0

이러한 오류 메시지는 검색 및 복사 - 붙여 넣기가 훨씬 쉬워 지므로 이미지가 아닌 텍스트로 게시하십시오. –

+1

SOAPAction은'Addition'으로 설정되어 있습니다. 실제 SOAP 몸체는'HelloWorld'를 인자로 가지지 만 서버 메소드는'getAccount'입니다. 이 세 가지가 일치하지 않는 한 결코 작동하지 않습니다. –

답변

0

제대로 이해했다면 REST 구현에 적합한 WebApi와 함께 SOAP을 사용하고 싶습니다. 일반적으로 SOAP가 필요한 경우 WCF를 사용하려고합니다.

어쨌든 클라이언트가 xml을 보내고 있지만 서비스가 구문 분석하여 accNbr 매개 변수를 추출하는 부분이 보이지 않으며 위에 언급 한 xml에 accNbr이 표시되지 않습니다.

또한 고객님의 getAccount 메소드가 Post 메소드로 공개되는지 확실하지 않습니다.

+0

웹 응답을 결코 통과하지 못하기 때문에 전체 코드를 게시하지 않았습니다. 작은 테스트를하고 있는데 비누를 사용해야하는 더 큰 프로젝트가 있습니다. – michel

+0

예, 물론 클라이언트에서 웹 응답을 전달하지 않습니다. 오류는 서버에서 발생하며 웹 서비스에서 XML을 구문 분석해야합니다. 웹 서비스가 XML을 구문 분석합니까? 'getAccount'에서 코드를 추가 할 수 있습니까? 또한 클라이언트에서 'getAccount'를 호출하는 위치가 표시되지 않습니다. 'http : // localhost : 62325/BalanceInquiry.asmx/getAccount'와 같이 'CreateSOAPWebRequest'의 주소에 추가해야합니다. . 또한 webapi를 사용하고 Post 메소드를 노출하는 경우 이름을 지정해야합니다. PostGetAccount를 사용하여 'Post'접미사를 사용합니다. –

+0

나는 서버에서 XML을 파싱 할 필요가 없다. 글쎄, 내가 XML 부분을 호출하고있다 – michel