In this article, I am explaining, How to get a response using HttpWebRequest Class.
Below is Sample Sode
private string
GetResponse()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string sCompleteRequest = apiUrl + "/payments/v1/sessions";
HttpWebRequest oHttpWebRequest = WebRequest.Create(sCompleteRequest) as HttpWebRequest;
oHttpWebRequest.Method = "POST";
oHttpWebRequest.ContentType = "application/json";
string sCredential = "username:password";//pass credential here
byte[] bData = System.Text.Encoding.UTF8.GetBytes(sCredential);
string sAuthorization = Convert.ToBase64String(bData);
oHttpWebRequest.Headers.Add("Authorization", "Basic " +
sAuthorization);
Byte[] oRequestByte = System.Text.Encoding.UTF8.GetBytes(GetStringJson().ToString());
Stream oRequestStream = oHttpWebRequest.GetRequestStream();
oRequestStream.Write(oRequestByte, 0,
oRequestByte.Length);
oRequestStream.Close();
string sCompleteResponse = string.Empty;
using (HttpWebResponse oHttpWebResponse = oHttpWebRequest.GetResponse() as HttpWebResponse)
{
if (oHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
Stream oStream = oHttpWebResponse.GetResponseStream();
StreamReader oStreamReader = new StreamReader(oStream);
sCompleteResponse =
oStreamReader.ReadToEnd();
}
}
// Get values
from response
JObject oJObject = JObject.Parse(sCompleteResponse);
JToken oJToken = oJObject["order"];
int iOrderId = Numericcl.GetIntValue(oJToken["id"]);
string sStatus = Stringcl.GetValue(oJToken["status"]);
string sOldStatus = Stringcl.GetValue(oJToken["old_status"]);
string sDescription = Stringcl.GetValue(oJToken["description"]);
// another
way Get values from response
m_sClientToken = oJObject.SelectToken("client_token").Value<string>();
iOrderId = oJObject.SelectToken("order.id").Value<int>();
decimal dDayTemp = oJObject.SelectToken("list[0].temp.day").Value<decimal>();
return sCompleteResponse;
}