Send post Http request with Content Type application Json on C#

  1. public bool SendSms1(string sms, string mob, string aport) 
  2. {  
  3.     bool flag = false;  
  4.     string userName = "admin";  
  5.     string passWord = "admin";  
  6.     try   
  7.     {  
  8.         string Url = ConfigurationSettings.AppSettings["url1"].ToString();  
  9.           
  10.   
  11.   
  12.         HttpWebRequest httpWReq = (HttpWebRequest) WebRequest.Create(Url);  
  13.   
  14.         Encoding encoding = new UTF8Encoding();  
  15.         // string postData = "{\"data\":{\"number\":\"" + arrMobile + "\",\"text\":\"" + sms + "\"},\"port\":\"" + arrPort + "\"}}";  
  16.         string postData = "{\"number\":[\"" + mob + "\"],\"text\":\"" + sms + "\",\"port\":[\"" + aport + "\"]}";  
  17.         byte[] data = encoding.GetBytes(postData);  
  18.   
  19.         httpWReq.ProtocolVersion = HttpVersion.Version11;  
  20.         httpWReq.Method = "POST";  
  21.         httpWReq.ContentType = "application/json"//charset=UTF-8";  
  22.         //httpWReq.Headers.Add("X-Amzn-Type-Version",  
  23.         // "[email protected]");  
  24.         //httpWReq.Headers.Add("X-Amzn-Accept-Type",  
  25.         // "[email protected]");  
  26.   
  27.         string _auth = string.Format("{0}:{1}", userName, passWord);  
  28.         string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));  
  29.         string _cred = string.Format("{0} {1}""Basic", _enc);  
  30.         //httpWReq.Headers.Add(HttpRequestHeader.Authorization,  
  31.         // "Bearer " + accessToken);  
  32.   
  33.         httpWReq.Headers[HttpRequestHeader.Authorization] = _cred;  
  34.         httpWReq.ContentLength = data.Length;  
  35.   
  36.   
  37.         Stream stream = httpWReq.GetRequestStream();  
  38.         stream.Write(data, 0, data.Length);  
  39.         stream.Close();  
  40.   
  41.         HttpWebResponse response = (HttpWebResponse) httpWReq.GetResponse();  
  42.         string s = response.ToString();  
  43.         StreamReader reader = new StreamReader(response.GetResponseStream());  
  44.         String jsonresponse = "";  
  45.         String temp = null;  
  46.         while ((temp = reader.ReadLine()) != null) {  
  47.             jsonresponse += temp;  
  48.         }  
  49.         return true;  
  50.     }   
  51.     catch (WebException e)   
  52.     {  
  53.   
  54.         using(WebResponse response = e.Response)   
  55.         {  
  56.             HttpWebResponse httpResponse = (HttpWebResponse) response;  
  57.             Console.WriteLine("Error code: {0}", httpResponse.StatusCode);  
  58.             using(Stream data = response.GetResponseStream())  
  59.             using(var reader = new StreamReader(data))   
  60.             {  
  61.                 string text = reader.ReadToEnd();  
  62.                 Console.WriteLine(text);  
  63.             }  
  64.         }  
  65.         return flag;  
  66.     }  
  67. }