Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. private Tuple<int, string> SendData2(string JsonData, string url, string username, string password)
  2. {
  3. UTF8Encoding encoding = new UTF8Encoding();
  4. //string url = "http://rtflistener-u001.elasticbeanstalk.com/Listener.ashx";
  5. //string username = "username";
  6. //string password = "password";
  7.  
  8. HttpWebRequest httpWebReq = (HttpWebRequest)WebRequest.Create(url);
  9.  
  10. string requestString = "username=" + username + "&password=" + password + "&data=" + JsonData;
  11.  
  12. byte[] cData = encoding.GetBytes(requestString);
  13.  
  14. httpWebReq.Method = "POST";
  15. httpWebReq.ContentType = "application/x-www-form-urlencoded";
  16. httpWebReq.ContentLength = cData.Length;
  17. httpWebReq.Timeout = 30000;
  18.  
  19. string responseString = "";
  20. int statusCode = 0;
  21.  
  22. try
  23. {
  24. using (Stream stream = httpWebReq.GetRequestStream())
  25. {
  26. stream.Write(cData, 0, cData.Length);
  27. }
  28.  
  29. HttpWebResponse httpResponse = (HttpWebResponse)httpWebReq.GetResponse();
  30. responseString = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd();
  31. statusCode= (int)httpResponse.StatusCode;
  32. //200
  33. }
  34. catch (WebException ex)
  35. {
  36. statusCode = (int)ex.Status;
  37. HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
  38. responseString = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd();
  39. //400, 500
  40. }
  41. catch (Exception ex)
  42. {
  43. }
  44.  
  45. return new Tuple<int, string>(statusCode, responseString);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement