Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. protected void btnCallPOSTwebService_Click(object sender, EventArgs e)
  2. {
  3. // The user has clicked on the "Call POST web service" button
  4. try
  5. {
  6. string WebServiceURL = tbWebServiceURL.Text;
  7.  
  8. // Convert our JSON in into bytes using ascii encoding
  9. ASCIIEncoding encoding = new ASCIIEncoding();
  10. byte[] data = encoding.GetBytes(tbJSONdata.Text);
  11.  
  12. // HttpWebRequest
  13. HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(WebServiceURL);
  14. webrequest.Method = "POST";
  15. webrequest.ContentType = "application/x-www-form-urlencoded";
  16. webrequest.ContentLength = data.Length;
  17.  
  18. // Get stream data out of webrequest object
  19. Stream newStream = webrequest.GetRequestStream();
  20. newStream.Write(data, 0, data.Length);
  21. newStream.Close();
  22.  
  23. // Declare & read the response from service
  24. HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
  25.  
  26. // Fetch the response from the POST web service
  27. Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
  28. StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
  29. string result = loResponseStream.ReadToEnd();
  30. loResponseStream.Close();
  31.  
  32. webresponse.Close();
  33.  
  34. txtResult.Text = result;
  35. }
  36. catch (Exception ex)
  37. {
  38. txtResult.Text = "An exception was thrown: " + ex.Message;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement