Home > Net >  Rookie question
Rookie question

Time:10-16

there is a class, how to call for the phone to send a text message
//phone number
Public string strMobile;
Private string strjson;
//the default of 86
Public string nationcode="86";
//random number
Private string strRand;
//get the signature this is the default template
Private int tpl_id=279204;
//the default appkey
Private string strAppKey="249 eee5a8e57b4c46233c2452318dee8";
//encryption sig
Private string sig.
//time
Private long strTime;
//sdkappid
Private int sdkappid=1400172942;
//request path
Private string url="https://yun.tim.qq.com/v5/tlssmssvr/sendsms?" ;
Public Message (string Mobile)
{
Enclosing strMobile=Mobile;
//initialize the random number
Enclosing GetRandomIntance ();
//initialization time
Enclosing GetCurrentTimeUnix ();
//get the current path
Enclosing geturl ();

//get the encrypted sig
This sig=GetSHA256HashFromString (the string. Format (" appkey={0} & amp; The random={1} & amp; Time={2} & amp; Mobile={3} ", enclosing strAppKey, enclosing strRand, enclosing strTime, enclosing strMobile));
//stitching json
Enclosing strjson="{" ext \ ": " \ ", \ "among " : \ "", \ "params " : [strRand \ "" + +" \ ", \ "3 " + "
"\]," sig \ ", \ "" + this. Sig +" \ ", \ "tel " : {\ "mobile " : \ "" + this strMobile +" \ ", \ "nationcode " : \ "" + this nationcode +" \ "}, \ "time ", \ "" + enclosing strTime +" \ ", \ "tpl_id ", \ "" + enclosing tpl_id +" \ "} ";


}

Public string sendMessage ()
{
String result=this. HttpWebResponse1 (this url, enclosing strjson);
//return JSON
return result;

}
//stitching url
Private void geturl ()
{
Enclosing the url=this. Url + "sdkappid=" + sdkappid + "& amp; The random="+ strRand;
}
//get a random number
Private void GetRandomIntance ()
{
Enclosing strRand=new Random (). Next, (10000, 99999). The ToString ();
}

///& lt; Summary>
///
///sha256 algorithm
///& lt;/summary>
///& lt; Param name="strData & gt;" The incoming need encryption string
///& lt; Returns> The encrypted code & lt;/returns>
Private static string GetSHA256HashFromString (string strData)
{
Byte [] bytValue=https://bbs.csdn.net/topics/System.Text.Encoding.UTF8.GetBytes (strData);
Try
{
SHA256 SHA256=new SHA256CryptoServiceProvider ();
Byte [] retVal=sha256.Com puteHash (bytValue);
StringBuilder sb=new StringBuilder();
for (int i=0; i {
Sb. Append (retVal [I]. ToString (" x2 "));
}
Return sb. ToString ();
}
The catch (Exception ex)
{
Throw new Exception (" GetSHA256HashFromString () fail, the error: "+ ex. Message);
}
}
///& lt; Summary>
///get the current time timeunix
///& lt;/summary>
///& lt; Returns> A timeunix
Public void GetCurrentTimeUnix ()
{
TimeSpan cha=(DateTime. Now - TimeZone. CurrentTimeZone. ToLocalTime (new System. A DateTime (1970, 1, 1)));
StrTime=(long) cha. TotalSeconds;
}

///& lt; Summary>
///send the request
///& lt;/summary>
///& lt; Param name="url" & gt; Path & lt;/param>
///& lt; Param name="json" & gt; Send json
///& lt; Returns>
Public string httpWebResponse1 (url string, the string json)
{
String returnstr="";
HttpWebRequest request=(HttpWebRequest) WebRequest. Create (url);
Request. The Method="POST";
Request. The ContentType="application/x - WWW - form - urlencoded";
Byte [] data=https://bbs.csdn.net/topics/Encoding.UTF8.GetBytes (json);
Request. ContentLength=data. Length;
Using (Stream reqStream=request. GetRequestStream ())
{
ReqStream. Write (data, 0, the data Length);
ReqStream. Close ();
}
HttpWebResponse respose=(HttpWebResponse) request. The method GetResponse ();
The Stream Stream=respose. GetResponseStream ();
Using (StreamReader reader=new StreamReader (stream, Encoding UTF8))
{
Returnstr=reader. ReadToEnd ();
}
Return returnstr;

}

CodePudding user response:

 
String mobile="15555555555";//do you want to send a phone number
The Message messageClient=new Message (mobile);
MessageClient. SendMessage ();
  •  Tags:  
  • C#
  • Related