Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private XmlDocument CallWebService(string method, string operation, string xmlPayload)
- {
- string result = "";
- string CREDENTIALS = "PASSword123";
- string URL_ADDRESS = "http://www.client.com/_ws/" + method + "?sso=" + CREDENTIALS + "&o=" + operation +; //TODO: customize to your needs
- // ===== You shoudn't need to edit the lines below =====
- // Create the web request
- HttpWebRequest request = WebRequest.Create(new Uri(URL_ADDRESS)) as HttpWebRequest;
- // Set type to POST
- request.Method = "POST";
- request.ContentType = "application/xml";
- // Create the data we want to send
- StringBuilder data = new StringBuilder();
- data.Append(xmlPayload);
- byte[] byteData = Encoding.UTF8.GetBytes(data.ToString()); // Create a byte array of the data we want to send
- request.ContentLength = byteData.Length; // Set the content length in the request headers
- // Write data to request
- using (Stream postStream = request.GetRequestStream())
- {
- postStream.Write(byteData, 0, byteData.Length);
- }
- // Get response and return it
- XmlDocument xmlResult = new XmlDocument();
- try
- {
- using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
- {
- StreamReader reader = new StreamReader(response.GetResponseStream());
- result = reader.ReadToEnd();
- reader.Close();
- }
- xmlResult.LoadXml(result);
- }
- catch (Exception e)
- {
- xmlResult = CreateErrorXML(e.Message, ""); //TODO: returns an XML with the error message
- }
- return xmlResult;
- }
- <soapenv:Envelope xmlns:ser="http://service.sunat.gob.pe" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
- <soapenv:Header>
- <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
- <wsse:UsernameToken wsu:Id="ABC-123">
- <wsse:Username>USERNAME</wsse:Username>
- <wsse:Password>PASSWORD</wsse:Password>
- </wsse:UsernameToken>
- </wsse:Security>
- </soapenv:Header>
- <soapenv:Body>
- <ser:sendBill>
- <fileName>20132263544-20-R001-0005649.zip</fileName>
- <contentFile>UEsDBC0AAAAIAOdhLUhAR3s5</contentFile>
- </ser:sendBill>
- </soapenv:Body>
- </soapenv:Envelope>
- public class PasswordDigestMessageInspector : IClientMessageInspector
- {
- public string Username { get; set; }
- public string Password { get; set; }
- public PasswordDigestMessageInspector(string username, string password)
- {
- Username = username;
- Password = password;
- }
- #region IClientMessageInspector Members
- public void AfterReceiveReply(ref Message reply, object correlationState)
- {
- return;
- }
- public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel)
- {
- UsernameToken token = new UsernameToken(this.Username, this.Password, PasswordOption.SendHashed);
- XmlElement securityToken = token.GetXml(new XmlDocument());
- MessageHeader securityHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", securityToken, false);
- request.Headers.Add(securityHeader);
- return Convert.DBNull;
- }
- #endregion
- }
- public class PasswordDigestBehavior : IEndpointBehavior
- {
- public string Usuario { get; set; }
- public string Password { get; set; }
- public PasswordDigestBehavior(string username, string password)
- {
- Usuario = username;
- Password = password;
- }
- public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
- {
- return;
- }
- public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
- {
- clientRuntime.ClientMessageInspectors.Add(new PasswordDigestMessageInspector(Usuario, Password));
- }
- public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
- {
- return;
- }
- public void Validate(ServiceEndpoint endpoint)
- {
- // Todo bien.
- return;
- }
- }
- <system.serviceModel>
- <bindings>
- <basicHttpBinding>
- <binding name="SunatBinding">
- <security mode="Transport" />
- </binding>
- </basicHttpBinding>
- </bindings>
- <client>
- <endpoint address="https://www.sunat.gob.pe:443/ol-ti-itemision-otroscpe-gem-beta/billService"
- binding="basicHttpBinding" bindingConfiguration="SunatBinding"
- contract="Sunat.billService" name="ServicioSunat" />
- </client>
- </system.serviceModel>
- public class EnvioSunat
- {
- Sunat.billServiceClient proxy;
- public Connect(string username, string password)
- {
- // Indicamos el nombre del Endpoint
- proxy = new Sunat.billServiceClient("ServicioSunat");
- // Agregamos el behavior configurado para soportar WS-Security.
- var behavior = new PasswordDigestBehavior(username, password);
- proxy.Endpoint.EndpointBehaviors.Add(behavior);
- // Abrimos el servicio.
- proxy.Open();
- /*
- .....
- Ejecutamos el código que llame a alguna operacion del Servicio.
- .....
- */
- proxy.Close(); // Cerramos la conexión.
- }
- }
- UsernameToken token = new UsernameToken(userName, txtPassword.Text, PasswordOption.SendPlainText);
Add Comment
Please, Sign In to add comment