Guest User

Untitled

a guest
Jul 8th, 2018
1,561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. private XmlDocument CallWebService(string method, string operation, string xmlPayload)
  2. {
  3. string result = "";
  4. string CREDENTIALS = "PASSword123";
  5. string URL_ADDRESS = "http://www.client.com/_ws/" + method + "?sso=" + CREDENTIALS + "&o=" + operation +; //TODO: customize to your needs
  6. // ===== You shoudn't need to edit the lines below =====
  7.  
  8. // Create the web request
  9. HttpWebRequest request = WebRequest.Create(new Uri(URL_ADDRESS)) as HttpWebRequest;
  10.  
  11. // Set type to POST
  12. request.Method = "POST";
  13. request.ContentType = "application/xml";
  14.  
  15. // Create the data we want to send
  16. StringBuilder data = new StringBuilder();
  17. data.Append(xmlPayload);
  18. byte[] byteData = Encoding.UTF8.GetBytes(data.ToString()); // Create a byte array of the data we want to send
  19. request.ContentLength = byteData.Length; // Set the content length in the request headers
  20.  
  21. // Write data to request
  22. using (Stream postStream = request.GetRequestStream())
  23. {
  24. postStream.Write(byteData, 0, byteData.Length);
  25. }
  26.  
  27. // Get response and return it
  28. XmlDocument xmlResult = new XmlDocument();
  29. try
  30. {
  31. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  32. {
  33. StreamReader reader = new StreamReader(response.GetResponseStream());
  34. result = reader.ReadToEnd();
  35. reader.Close();
  36. }
  37. xmlResult.LoadXml(result);
  38. }
  39. catch (Exception e)
  40. {
  41. xmlResult = CreateErrorXML(e.Message, ""); //TODO: returns an XML with the error message
  42. }
  43. return xmlResult;
  44. }
  45.  
  46. <soapenv:Envelope xmlns:ser="http://service.sunat.gob.pe" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  47. <soapenv:Header>
  48. <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">
  49. <wsse:UsernameToken wsu:Id="ABC-123">
  50. <wsse:Username>USERNAME</wsse:Username>
  51. <wsse:Password>PASSWORD</wsse:Password>
  52. </wsse:UsernameToken>
  53. </wsse:Security>
  54. </soapenv:Header>
  55. <soapenv:Body>
  56. <ser:sendBill>
  57. <fileName>20132263544-20-R001-0005649.zip</fileName>
  58. <contentFile>UEsDBC0AAAAIAOdhLUhAR3s5</contentFile>
  59. </ser:sendBill>
  60. </soapenv:Body>
  61. </soapenv:Envelope>
  62.  
  63. public class PasswordDigestMessageInspector : IClientMessageInspector
  64. {
  65. public string Username { get; set; }
  66. public string Password { get; set; }
  67.  
  68. public PasswordDigestMessageInspector(string username, string password)
  69. {
  70. Username = username;
  71. Password = password;
  72. }
  73.  
  74. #region IClientMessageInspector Members
  75.  
  76. public void AfterReceiveReply(ref Message reply, object correlationState)
  77. {
  78. return;
  79. }
  80.  
  81. public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel)
  82. {
  83. UsernameToken token = new UsernameToken(this.Username, this.Password, PasswordOption.SendHashed);
  84.  
  85. XmlElement securityToken = token.GetXml(new XmlDocument());
  86.  
  87. MessageHeader securityHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", securityToken, false);
  88. request.Headers.Add(securityHeader);
  89.  
  90. return Convert.DBNull;
  91. }
  92.  
  93. #endregion
  94. }
  95.  
  96. public class PasswordDigestBehavior : IEndpointBehavior
  97. {
  98.  
  99. public string Usuario { get; set; }
  100. public string Password { get; set; }
  101.  
  102. public PasswordDigestBehavior(string username, string password)
  103. {
  104. Usuario = username;
  105. Password = password;
  106. }
  107.  
  108. public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
  109. {
  110. return;
  111. }
  112.  
  113. public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  114. {
  115. clientRuntime.ClientMessageInspectors.Add(new PasswordDigestMessageInspector(Usuario, Password));
  116. }
  117.  
  118. public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  119. {
  120. return;
  121. }
  122.  
  123. public void Validate(ServiceEndpoint endpoint)
  124. {
  125. // Todo bien.
  126. return;
  127. }
  128. }
  129.  
  130. <system.serviceModel>
  131. <bindings>
  132. <basicHttpBinding>
  133. <binding name="SunatBinding">
  134. <security mode="Transport" />
  135. </binding>
  136. </basicHttpBinding>
  137. </bindings>
  138. <client>
  139. <endpoint address="https://www.sunat.gob.pe:443/ol-ti-itemision-otroscpe-gem-beta/billService"
  140. binding="basicHttpBinding" bindingConfiguration="SunatBinding"
  141. contract="Sunat.billService" name="ServicioSunat" />
  142. </client>
  143. </system.serviceModel>
  144.  
  145. public class EnvioSunat
  146. {
  147. Sunat.billServiceClient proxy;
  148.  
  149. public Connect(string username, string password)
  150. {
  151. // Indicamos el nombre del Endpoint
  152. proxy = new Sunat.billServiceClient("ServicioSunat");
  153.  
  154. // Agregamos el behavior configurado para soportar WS-Security.
  155. var behavior = new PasswordDigestBehavior(username, password);
  156. proxy.Endpoint.EndpointBehaviors.Add(behavior);
  157.  
  158. // Abrimos el servicio.
  159. proxy.Open();
  160. /*
  161. .....
  162. Ejecutamos el código que llame a alguna operacion del Servicio.
  163. .....
  164. */
  165.  
  166. proxy.Close(); // Cerramos la conexión.
  167. }
  168. }
  169.  
  170. UsernameToken token = new UsernameToken(userName, txtPassword.Text, PasswordOption.SendPlainText);
Add Comment
Please, Sign In to add comment