Guest User

Untitled

a guest
Apr 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Web.Script.Serialization;
  11. using System.Windows.Forms;
  12. using System.Threading;
  13.  
  14. namespace WindowsFormsApp1
  15. {
  16. public partial class Form1 : Form
  17. {
  18.  
  19. private static ManualResetEvent allDone = new ManualResetEvent(false);
  20. static byte[] byteArray = null;
  21. static string transactioninfo = string.Empty;
  22.  
  23. public Form1()
  24. {
  25. InitializeComponent();
  26. }
  27.  
  28. private void Form1_Load(object sender, EventArgs e)
  29. {
  30.  
  31. Transaction t = new Transaction();
  32. t.branch = "PagoEnLinea";
  33. t.description = "Test";
  34. t.entity = "GBCSAprovechamiento";
  35. t.purchaseid = DateTime.Now.ToString("ddMMyyyyHHmmss");
  36. t.total = "112345";
  37. t.version = "2";
  38.  
  39. // Ejemplo IOnicial String res=Pagar(t);
  40. String res = Llamar_Pagar(t);
  41.  
  42. }
  43.  
  44. public class Transaction
  45. {
  46. public string purchaseid { get; set; }
  47. public string total { get; set; }
  48. public string version { get; set; }
  49. public string entity { get; set; }
  50. public string branch { get; set; }
  51. public string description { get; set; }
  52.  
  53. public Transaction()
  54. {
  55. purchaseid = "";
  56. total = "";
  57. version = "";
  58. entity = "";
  59. branch = "";
  60. description = "";
  61. }
  62. }
  63.  
  64. public class nResponse
  65. {
  66. public int error { get; set; }
  67. public string errorDesc { get; set; }
  68. public string timeIni { get; set; }
  69. public DateTime _tIni { get; set; }
  70. public string timeEnd { get; set; }
  71. public DateTime _tEnd { get; set; }
  72. public string nData { get; set; }
  73.  
  74. public nResponse()
  75. {
  76. error = 0;
  77. errorDesc = "";
  78. timeIni = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
  79. timeEnd = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
  80. nData = "";
  81. }
  82. }
  83.  
  84. public string Llamar_Pagar(Transaction obj)
  85. {
  86. string strRegresa = string.Empty;
  87.  
  88. // Declaración de Variables
  89. string respuesta = "";
  90. string url = "https://pcservicebsd.azurewebsites.net/frm/pago";
  91. string username = "CompraC&A";
  92. string password = "FgtyuO98Dca!=";
  93.  
  94.  
  95. //Preparacion de Datos a Enviar.
  96. JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
  97. string datos = String.Concat("transactioninfo:", jsSerializer.Serialize(obj));
  98. transactioninfo = datos;
  99. byteArray = Encoding.UTF8.GetBytes(transactioninfo);
  100.  
  101. //Preparacion de Encabezado de solicitud
  102. CredentialCache mycache = new CredentialCache();
  103. mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
  104.  
  105. //Preparacion de Acceso a Servicio de Pago
  106. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  107. request.UserAgent = "Apache-HttpClient/4.2.1";
  108. request.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
  109. //1 request.Headers.Add("Signature", CanonicalReq);
  110. request.Headers.Add("SignedHeaders", "host");
  111. request.ProtocolVersion = HttpVersion.Version11;
  112. //0 request.Credentials = mycache;
  113. request.Headers.Add("Authorization", "Basic Q29tcHJhQyZBOkZndHl1Tzk4RGNhIT0="); // "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + ":" + password)));
  114. request.ContentType = "application/x-www-form-urlencoded";
  115. request.Method = "POST";
  116. //0 request.ContentLength = transactioninfo.Length;
  117.  
  118. //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  119.  
  120. request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
  121.  
  122. allDone.WaitOne();
  123.  
  124. return strRegresa;
  125. }
  126.  
  127. private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
  128. {
  129. HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
  130.  
  131. // End the operation
  132. Stream postStream = request.EndGetRequestStream(asynchronousResult);
  133. /*
  134. Console.WriteLine("Please enter the input data to be posted:");
  135. string postData = Console.ReadLine();
  136.  
  137. // Convert the string into a byte array.
  138. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  139.  
  140. // Write to the request stream.
  141. postStream.Write(byteArray, 0, postData.Length);
  142. */
  143. postStream.Write(byteArray, 0, transactioninfo.Length);
  144. postStream.Close();
  145.  
  146. // Start the asynchronous operation to get the response
  147. request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
  148. }
  149.  
  150. private static void GetResponseCallback(IAsyncResult asynchronousResult)
  151. {
  152. HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
  153.  
  154. // End the operation
  155. HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
  156. Stream streamResponse = response.GetResponseStream();
  157. StreamReader streamRead = new StreamReader(streamResponse);
  158. string responseString = streamRead.ReadToEnd();
  159. Console.WriteLine(responseString);
  160. // Close the stream object
  161. streamResponse.Close();
  162. streamRead.Close();
  163.  
  164. // Release the HttpWebResponse
  165. response.Close();
  166. allDone.Set();
  167. }
  168.  
  169. public string Pagar(Transaction obj)
  170. {
  171. JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
  172. string respuesta = "";
  173. string url = "https://pcservicebsd.azurewebsites.net/frm/pago";
  174. //url = "http://localhost:64987/frm/pago";
  175. string username = "CompraC&A";
  176. string password = "FgtyuO98Dca!=";
  177.  
  178. CredentialCache mycache = new CredentialCache();
  179.  
  180. try
  181. {
  182.  
  183. string datos = String.Concat("transactioninfo:", jsSerializer.Serialize(obj));
  184. mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
  185. WebRequest request = WebRequest.Create(url);
  186.  
  187. request.Credentials = mycache;
  188. request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + ":" + password)));
  189. request.Method = "POST";
  190. string transactioninfo = datos;
  191. byte[] byteArray = Encoding.UTF8.GetBytes(transactioninfo);
  192. request.ContentType = "application/x-www-form-urlencoded";
  193. request.ContentLength = byteArray.Length;
  194. Stream dataStream = request.GetRequestStream();
  195. dataStream.Write(byteArray, 0, byteArray.Length);
  196. dataStream.Close();
  197. WebResponse response = request.GetResponse();
  198. Console.WriteLine(((HttpWebResponse)response).StatusDescription);
  199. using (dataStream = response.GetResponseStream())
  200. {
  201. StreamReader reader = new StreamReader(dataStream);
  202. respuesta = reader.ReadToEnd();
  203. }
  204.  
  205. nResponse respuestaT = jsSerializer.Deserialize<nResponse>(respuesta);
  206. response.Close();
  207. //
  208. }
  209. catch (Exception msgError)
  210. {
  211. respuesta = msgError.Message;
  212. }
  213.  
  214. return respuesta;
  215. }
  216.  
  217. }
  218. }
Add Comment
Please, Sign In to add comment