This article is mainly in the C# Core Web API framework to achieve the function of sending verification code SMS to the phone. Here I choose to be a Huyi wireless SMS verification code platform, in fact, like Ali cloud, Tencent cloud can also be above the。
- First, let's go to HootSuite./api/ Go sign up for an account.
After completing the account registration, it will send 10 free SMS and call verification code(ps: I don't have 10 on here because I've already used it. New people have 10.)
2. The following code begins to first create a class
3. Below directly on the code
using System;
using ;
using ;
using ;
using ;
using ;
namespace
{
public class SendSmsUtil
{
private static readonly string URL = "/webservice/?method=Submit"; // Domestic request path
private static readonly string APPID = "Fill in your own APPID here"; // Fill in your own APPID here
private static readonly string APIKEY = "Fill in your own APIKEY here"; // Fill in your own APIKEY here
public static async Task<string> SendSmsAsync(string number)
{
using (var client = new HttpClient())
{
// random number
Random random = new Random();
int mobileCode = (100000, 999999); // Generate a six-digit random number
string content = $"Your verification code is:{mobileCode}。Please don't give out the verification code to others。";
var parameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("account", APPID),
new KeyValuePair<string, string>("password", APIKEY),
new KeyValuePair<string, string>("mobile", number),
new KeyValuePair<string, string>("content", content)
};
var contentToSend = new FormUrlEncodedContent(parameters);
try
{
var response = await (URL, contentToSend);
var responseBody = await ();
// analyze XML responsive
// analyze XML
XDocument xmlDoc = (responseBody);
// through (a gap) XML Access to information in
var code = (("code", "/"))?.Value;
var msg = (("msg", "/"))?.Value;
var smsid = (("smsid", "/"))?.Value;
($"code: {code}");
($"msg: {msg}");
($"smsid: {smsid}");
($"mo: {mobileCode}");
if (code == "2")
{
("SMS Submission Successful");
return ();
}
}
catch (Exception ex)
{
();
}
return "";
}
}
}
}
And apikey check out this place.
5. The following is the code needed in the controller
`using ;
using ;
using ;
namespace
{
[Route("api/[controller]")]
[ApiController]
public class SmsController : ControllerBase
{
[HttpPost("send")]
public async Task
{
if ((phoneNumber))
{
return BadRequest("Cell phone number can't be null");;
}
var result = await (phoneNumber);
if ((result))
{
return StatusCode(500, "Failed to send SMS");
}
return Ok(new { VerificationCode = result });
}
}
}`
6. enter the cell phone number and test the following is a successful result cell phone and can be verified by the code
The above content has realized the cell phone verification code function. The code mainly refers to the official website code and AI generation and, there may be some statement problems thank you for your guidance and suggestions!
Reprinted with permission. Thank you!
Instructed by Shijie Zhu (@Twolp)