Advertisement
Guest User

Untitled

a guest
Jul 27th, 2015
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. ASCIIEncoding encoding = new ASCIIEncoding();
  2. Byte[] postBytes = encoding.GetBytes(jsondata);
  3. // used to build entire input
  4. StringBuilder sb = new StringBuilder();
  5.  
  6. // used on each read operation
  7. byte[] buf = new byte[8192];
  8.  
  9. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(geturl);
  10. //request.Credentials = new NetworkCredential(supplierid, token);
  11. request.Method = "POST";
  12. request.Accept = "application/json";
  13. request.UserAgent = "curl/7.37.0";
  14. request.ContentLength = postBytes.Length;
  15. request.ContentType = "application/json";
  16. SetBasicAuthHeader(request, supplierid, token);
  17. Stream postStream = request.GetRequestStream();
  18. postStream.Write(postBytes, 0, postBytes.Length);
  19. postStream.Close();
  20.  
  21. try
  22. {
  23. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  24. Stream resStream = response.GetResponseStream();
  25.  
  26. string tempString = null;
  27. int count = 0;
  28. do
  29. {
  30. // fill the buffer with data
  31. count = resStream.Read(buf, 0, buf.Length);
  32.  
  33. // make sure we read some data
  34. if (count != 0)
  35. {
  36. // translate from bytes to ASCII text
  37. tempString = Encoding.ASCII.GetString(buf, 0, count);
  38. Console.WriteLine(tempString);
  39.  
  40. // continue building the string
  41. sb.Append(tempString);
  42. }
  43. }
  44. while (count > 0); // any more data to read?
  45. }
  46. catch (WebException ex)
  47. {
  48. Console.WriteLine(ex.ToString());
  49. logger.Log(ex.ToString());
  50. //Console.ReadLine();
  51. }
  52.  
  53. <?php
  54.  
  55.  
  56.  
  57. // Create a new order
  58.  
  59.  
  60.  
  61. $username = "foo@foodomain.com";
  62.  
  63. $password = "qaz@PASSword.net";
  64.  
  65.  
  66.  
  67. $base_url = "https://my.freestylecommerce.com/webapi/V1/";
  68.  
  69.  
  70.  
  71.  
  72.  
  73. $data_json ='{"OrderNumber":"123495","OrderDate":"01/07/2015 5:06:39 AM","ShippingMethod":"2-Day Domestic","BillingAddress":{"FirstName":"Joe","LastName":"Test","Company":"","AddressLine1":"9 Sevilla Rd","AddressLine2":"","City":"Andover","State":"MA","Postcode":"01810","Country":"US","Email":"joet@yahoo.com",
  74.  
  75. "Phone":"9785551234"},"ShippingAddress":{"FirstName":"Joe","LastName":"Test","Company":"","AddressLine1":"9 Sevilla Rd","AddressLine2":"","City":"Andover","State":"MA","Postcode":"01810","Country":"US","Email":"joet@yahoo.com",
  76.  
  77. "Phone":"9785551234"},"UpdateAt":"01/07/2015 5:06:39 AM","OrderStatus":12,"TotalAmount":22.70,"PaidAmount":0.0,"ShippingAmount":6.95,"TaxAmount":0.00,"OrderItems":
  78.  
  79. [{"ProductSKU":"PF7977","Quantity":"1.00","Price":"15.75","Discount":"0.00"}],"SalesChannelId":"f5defa6d-9566-4705-b201-a3e90170895a"}';
  80.  
  81.  
  82.  
  83. $url = $base_url . "Orders";
  84.  
  85. print("<br/><br/>***** POST to URL ". $url . "<br/>");
  86.  
  87. $out = post_request ($data_json, $url, $username, $password);
  88.  
  89. print($out);
  90.  
  91.  
  92.  
  93.  
  94.  
  95. /*
  96.  
  97. * Curl POST request
  98.  
  99. */
  100.  
  101. function post_request ($data_json, $url, $username, $password)
  102.  
  103. {
  104.  
  105. $CURL = curl_init();
  106.  
  107.  
  108.  
  109. // Set Curl Parameters
  110.  
  111. curl_setopt($CURL, CURLOPT_URL, $url); // set url to post to
  112.  
  113. curl_setopt($CURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  114.  
  115. curl_setopt($CURL, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json','Content-Length: ' . strlen($data_json)));
  116.  
  117. curl_setopt($CURL, CURLOPT_POST, 1);
  118.  
  119. curl_setopt($CURL, CURLOPT_POSTFIELDS,$data_json);
  120.  
  121. curl_setopt($CURL, CURLOPT_USERPWD, $username . ":" . $password);
  122.  
  123. curl_setopt($CURL, CURLOPT_RETURNTRANSFER, true);
  124.  
  125. curl_setopt($CURL, CURLOPT_SSL_VERIFYPEER, 0);
  126.  
  127. curl_setopt($CURL, CURLOPT_SSL_VERIFYHOST, 0);
  128.  
  129.  
  130.  
  131. // Curl Response
  132.  
  133. $Response = curl_exec($CURL);
  134.  
  135. if($Response === false)
  136.  
  137. {
  138.  
  139. print(curl_error($CURL));
  140.  
  141. }
  142.  
  143. curl_close($CURL);
  144.  
  145. return $Response;
  146.  
  147. }
  148.  
  149. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement