public bool SendSms1(string sms, string mob, string aport)
{
bool flag = false;
string userName = "admin";
string passWord = "admin";
try
{
string Url = ConfigurationSettings.AppSettings["url1"].ToString();
HttpWebRequest httpWReq = (HttpWebRequest) WebRequest.Create(Url);
Encoding encoding = new UTF8Encoding();
// string postData = "{\"data\":{\"number\":\"" + arrMobile + "\",\"text\":\"" + sms + "\"},\"port\":\"" + arrPort + "\"}}";
string postData = "{\"number\":[\"" + mob + "\"],\"text\":\"" + sms + "\",\"port\":[\"" + aport + "\"]}";
byte[] data = encoding.GetBytes(postData);
httpWReq.ProtocolVersion = HttpVersion.Version11;
httpWReq.Method = "POST";
httpWReq.ContentType = "application/json"; //charset=UTF-8";
//httpWReq.Headers.Add("X-Amzn-Type-Version",
// "[email protected]");
//httpWReq.Headers.Add("X-Amzn-Accept-Type",
// "[email protected]");
string _auth = string.Format("{0}:{1}", userName, passWord);
string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
string _cred = string.Format("{0} {1}", "Basic", _enc);
//httpWReq.Headers.Add(HttpRequestHeader.Authorization,
// "Bearer " + accessToken);
httpWReq.Headers[HttpRequestHeader.Authorization] = _cred;
httpWReq.ContentLength = data.Length;
Stream stream = httpWReq.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse) httpWReq.GetResponse();
string s = response.ToString();
StreamReader reader = new StreamReader(response.GetResponseStream());
String jsonresponse = "";
String temp = null;
while ((temp = reader.ReadLine()) != null) {
jsonresponse += temp;
}
return true;
}
catch (WebException e)
{
using(WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse) response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using(Stream data = response.GetResponseStream())
using(var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
return flag;
}
}This will guide you show to send post request with username and password where the content type is application/json.
