Saltar al contenido

Cliente para enviar solicitud SOAP y recibir respuesta

Te recomendamos que revises esta solución en un entorno controlado antes de enviarlo a producción, saludos.

Solución:

Normalmente uso otra forma de hacer lo mismo.

using System.Xml;
using System.Net;
using System.IO;

public static void CallWebService()

    var _url = "http://xxxxxxxxx/Service1.asmx";
    var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
    HttpWebRequest webRequest = CreateWebRequest(_url, _action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

    // begin async call to web request.
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

    // suspend this thread until call is complete. You might want to
    // do something usefull here like update your UI.
    asyncResult.AsyncWaitHandle.WaitOne();

    // get the response from the completed web request.
    string soapResult;
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
    
        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
        
            soapResult = rd.ReadToEnd();
        
        Console.Write(soapResult);        
    


private static HttpWebRequest CreateWebRequest(string url, string action)

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Headers.Add("SOAPAction", action);
    webRequest.ContentType = "text/xml;charset="utf-8"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;


private static XmlDocument CreateSoapEnvelope()

    XmlDocument soapEnvelopeDocument = new XmlDocument();
    soapEnvelopeDocument.LoadXml(
    @"
        
            
                12
                32
            
        
    ");
    return soapEnvelopeDocument;


private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)

    using (Stream stream = webRequest.GetRequestStream())
    
        soapEnvelopeXml.Save(stream);
    

Tengo esta solución simple aquí:

Envío de solicitud SOAP y recepción de respuesta en .NET 4.0 C# sin usar WSDL o clases de proxy:

class Program
    
        /// 
        /// Execute a Soap WebService call
        /// 
        public static void Execute()
        
            HttpWebRequest request = CreateWebRequest();
            XmlDocument soapEnvelopeXml = new XmlDocument();
            soapEnvelopeXml.LoadXml(@"
                
                  
                    
                  
                ");

            using (Stream stream = request.GetRequestStream())
            
                soapEnvelopeXml.Save(stream);
            

            using (WebResponse response = request.GetResponse())
            
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                
                    string soapResult = rd.ReadToEnd();
                    Console.WriteLine(soapResult);
                
            
        
        /// 
        /// Create a soap webrequest to [Url]
        /// 
        /// 
        public static HttpWebRequest CreateWebRequest()
        
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:56405/WebService1.asmx?op=HelloWorld");
            webRequest.Headers.Add(@"SOAP:Action");
            webRequest.ContentType = "text/xml;charset="utf-8"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        

        static void Main(string[] args)
        
            Execute();
        
    

Creo que hay una manera más simple:

 public async Task CreateSoapEnvelope()
 
      string soapString = @"
          
              
                  
              
          ";

          HttpResponseMessage response = await PostXmlRequest("your_url_here", soapString);
          string content = await response.Content.ReadAsStringAsync();

      return content;
 

 public static async Task PostXmlRequest(string baseUrl, string xmlString)
 
      using (var httpClient = new HttpClient())
      
          var httpContent = new StringContent(xmlString, Encoding.UTF8, "text/xml");
          httpContent.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld");

          return await httpClient.PostAsync(baseUrl, httpContent);
       
 

¡Haz clic para puntuar esta entrada!
(Votos: 2 Promedio: 4.5)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *