C# Corner
Tech
News
Videos
Forums
Trainings
Books
Live
More
Interviews
Events
Jobs
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Send post Http request with Content Type application Json on C#
WhatsApp
Pawan Negi
Apr 11
2015
93.5
k
0
4
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;
}
}
C# Programming
Http request
json
Up Next
Send post Http request with Content Type application Json on C#