I’ve an ready made link that will send an SMS message to user’s phone with THE OTP.
However, I’m using this way and I’m wondering if there is a better way to send the request. Also, how to make sure that I got an 200 OK response.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Close();
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use HttpClient.
Example:
public class Client
{
HttpClient _httpClient;
public Client()
{
_httpClient=new HttpClient();
}
//async post,get request
public async Task<string> sendGetRequest(string url)
{
var response= await _httpClient.GetAsync(url);
if(response.IsSuccessStatusCode)
{
//if response has status code 200 do something
//in this case I'm just converting the content of the response in string format
string result=await response.Content.ReadStringAsync();
}
else
{
return "error";
}
}
public async Task<string> sendPostRequest(string url,string bodyJson)
{
//Example sending a post request with json as body.
var body=new StringContent(bodyJson, Encoding.UTF8, "application/json");
var response= await _httpClient.PostAsync(url,body);
if(response.IsSuccessStatusCode)
{
//if response has status code 200 do something
//in this case I'm just converting the contntent of the response in string format
string result=await response.Content.ReadStringAsync();
return result;
}
else
{
return "error";
}
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0