0

Http-Post 접근 방식으로 웹 서비스에 요청하고 싶습니다. 다음은 C#의 코드입니다. 여기 내부 서버 오류를 제공하는 HttpWebRequest.GetResponse

 static void Main(string[] args) 
     { 
      var _url = "http://localhost/EventWebService/EventWebService.asmx"; 
      var _action = "http://localhost/EventWebService/EventWebService.asmx?op=GetPerson"; 

      XmlDocument soapEnvelope = new XmlDocument(); 
      soapEnvelope.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><GetPerson xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><name>Brad</name><surname>Pitt</surname><age>51</age></GetPerson></SOAP-ENV:Body></SOAP-ENV:Envelope>"); 

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url); 
      request.Headers.Add("SOAPAction", _action); 
      request.ContentType = "text/xml;charset=\"utf-8\""; 
      request.Accept = "text/xml"; 
      request.Method = "POST"; 
      InsertSoapEnvelopeIntoWebRequest(soapEnvelope, request); 
      WebResponse response = request.GetResponse(); 
      StreamReader rd = new StreamReader(response.GetResponseStream()); 
      Console.WriteLine(rd.ReadToEnd()); 
     } 

     private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) 
     { 
      using (Stream stream = webRequest.GetRequestStream()) 
      { 
       soapEnvelopeXml.Save(stream); 
      } 
     } 

내 웹 서비스 방법 및 사용 클래스 :

[WebMethod] 
    [ return: XmlElement("ReturnValueElement",IsNullable=false)] 
    public Person GetPerson([XmlElement("Name")] string name, [XmlElement("Surname")] string surname, [XmlElement("Age")] int age) 
    { 
     return new Person(name, surname, age); 
    } 

    public class Person 
    { 
     string Name { get; set; } 
     string Surname { get; set; } 
     int Age { get; set; } 

     public Person() { } 

     public Person(string _name, string _surname, int _age) 
     { 
      Name = _name; 
      Surname = _surname; 
      Age = _age; 
     } 
    }; 

그리고 여기에 웹 서비스를위한 설정 파일 내 구성입니다 :

홈페이지에서
<configuration> 
    <system.web> 
    <webServices> 
     <protocols> 
      <add name="HttpSoap"/>    
      <add name="Documentation"/> 
      <add name="HttpPostLocalhost"/> 
      <add name="HttpPost"/> 
     </protocols> 
    </webServices> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
    </system.web> 
</configuration> 

, 내가 WebResponse response = request.GetResponse();를 디버깅 라인, 내가 내부 서버 오류 (500) 프로토콜 오류 상태.

아이디어가 있으십니까? 다들 감사 해요.

+1

왜 당신은 일반 웹 참조 또는 WCF를 사용하지 않는과 함께이 코드? – fejesjoco

답변

0

사용 시도 및 캐치 방법

try 
{ 
    // Your Code 
} 

catch(Exception wex) 
{ 
    string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString(); 
    return pageContent; 
}