Advertisement
Guest User

img

a guest
Jul 17th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 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.Windows.Forms;
  11. using System.Threading.Tasks;
  12.  
  13.  
  14. namespace ConsoleApp1
  15. {
  16. class MiniHotelApi
  17. {
  18. private struct SCI_Document
  19. {
  20. public string ReservationNumber;
  21. public string DocType;
  22. public string DocValue;
  23. public string Description;
  24.  
  25. }
  26.  
  27.  
  28. public static string cmdSend_Click()
  29. {
  30. string path = @"C:\inetpub\wwwroot\rambam\system\images\scannedImage1.jpg";
  31. List<SCI_Document> lstDocuments = new List<SCI_Document>();
  32. lstDocuments.Add(new SCI_Document()
  33. {
  34. Description = "Test Example",
  35. DocType = Path.GetExtension(path).Replace(".", ""),
  36. DocValue = EncodeImage(path),
  37. ReservationNumber = "007000301"
  38. });
  39.  
  40.  
  41. string AgentUser = "rambam"; //MiniHotel Rambam agent username
  42. string AgentPass = "bam*700"; //MiniHotel Rambam agent password
  43. string HotelID = "rambam"; //MiniHotel Rambam HotelID
  44. string Response = SendRequest(getXMLRequest(AgentUser, AgentPass, HotelID, lstDocuments)); //Send Request Using RamBam agent credentials
  45.  
  46. return Response;
  47. //frmXML xmlResponseDialog = new frmXML(Response);
  48. // xmlResponseDialog.ShowDialog();
  49. // xmlResponseDialog.Dispose();
  50. } //Send Request Button
  51.  
  52. public static string EncodeImage(string path)
  53. {
  54. string EncodedImage = "";
  55. System.Drawing.Image img = System.Drawing.Image.FromFile(path);
  56.  
  57. try
  58. {
  59. EncodedImage = Convert.ToBase64String(ImageToByteArray(img));
  60. }
  61. catch (Exception ex)
  62. {
  63. }
  64. return EncodedImage;
  65. } //Encode Image Function
  66. public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
  67. {
  68. MemoryStream ms = new MemoryStream();
  69.  
  70. imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  71. return ms.ToArray();
  72. } //Converts image to ByteArray
  73.  
  74. /// <summary>
  75. /// send
  76. /// </summary>
  77. /// <param name="sender"></param>
  78. /// <param name="e"></param>
  79.  
  80. #region "Server Functions"
  81. private static string SendRequest(string postdata)
  82. {
  83.  
  84. HttpWebRequest s;
  85. UTF8Encoding enc;
  86. byte[] postdatabytes;
  87.  
  88. string ServerURL = "http://sandbox.hotel-wiz.com/";
  89. if (ServerURL.EndsWith("/"))
  90. ServerURL = ServerURL.Substring(0, ServerURL.Length - 1);
  91. string FunctionURL = ServerURL + "/agents/ws/sci/sciMain.asmx/saveDocuments";
  92.  
  93. s = (HttpWebRequest)WebRequest.Create(FunctionURL);
  94. enc = new System.Text.UTF8Encoding();
  95. postdatabytes = enc.GetBytes(postdata);
  96. s.Method = "POST";
  97. s.ContentType = "application/xml";
  98. s.ContentLength = postdatabytes.Length;
  99.  
  100. using (var stream = s.GetRequestStream())
  101. {
  102. stream.Write(postdatabytes, 0, postdatabytes.Length);
  103. }
  104. var result = s.GetResponse();
  105. string responseStr = "";
  106.  
  107. try
  108. {
  109. StreamReader reader = new StreamReader(result.GetResponseStream());
  110. responseStr = reader.ReadToEnd();
  111. reader.Close();
  112. }
  113. catch (Exception ex)
  114. {
  115. }
  116.  
  117. return responseStr;
  118. } //POST function to send XML Request
  119.  
  120. private static string getXMLRequest(string Username, string Password, string HotelId, List<SCI_Document> lstDocuments)
  121. {
  122. string strXML = "";
  123.  
  124. strXML += "<?xml version='1.0' encoding='UTF-8'?>"; //Create XML Header
  125. strXML += "<Request>"; //Create 'Request' Node
  126. strXML += "<SCI name='sci_saveDocuments'>"; //Create 'SCI' Node
  127. strXML += "<Authentication username='" + Username + "' password='" + Password + "' />"; //Create 'Authentication' Node
  128. strXML += "<Hotel id='" + HotelId + "' />"; //Create 'Hotel' Node
  129. strXML += "<Documents>"; //Create 'Documents' Node
  130. foreach (SCI_Document d in lstDocuments) //Iterate throught Documents List
  131. strXML += "<Document rs_number='" + d.ReservationNumber //Create 'Document' Node
  132. + "' Doc_type='" + d.DocType
  133. + "' Doc_value='" + d.DocValue
  134. + "' Description='" + d.Description + "' />";
  135.  
  136. strXML += "</Documents>"; //End of 'Documents' Node
  137. strXML += "</SCI>"; //End of 'SCI' Node
  138. strXML += "</Request>"; //End of 'Request' Node
  139. return strXML; //Return created XML Request
  140. } //Function that creates saveDocuments() XML Request
  141.  
  142. #endregion
  143.  
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement