Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void btnCallPOSTwebService_Click(object sender, EventArgs e)
- {
- // The user has clicked on the "Call POST web service" button
- try
- {
- string WebServiceURL = tbWebServiceURL.Text;
- // Convert our JSON in into bytes using ascii encoding
- ASCIIEncoding encoding = new ASCIIEncoding();
- byte[] data = encoding.GetBytes(tbJSONdata.Text);
- // HttpWebRequest
- HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(WebServiceURL);
- webrequest.Method = "POST";
- webrequest.ContentType = "application/x-www-form-urlencoded";
- webrequest.ContentLength = data.Length;
- // Get stream data out of webrequest object
- Stream newStream = webrequest.GetRequestStream();
- newStream.Write(data, 0, data.Length);
- newStream.Close();
- // Declare & read the response from service
- HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
- // Fetch the response from the POST web service
- Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
- StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
- string result = loResponseStream.ReadToEnd();
- loResponseStream.Close();
- webresponse.Close();
- txtResult.Text = result;
- }
- catch (Exception ex)
- {
- txtResult.Text = "An exception was thrown: " + ex.Message;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement