SMS Verification (JAVA)

Source: Internet
Author: User

Package com.tq.jjb.common.util;

Import java.io.IOException;
Import Java.security.MessageDigest;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;

Import Net.sf.json.JSONObject;

Import Org.apache.http.HttpResponse;
Import org.apache.http.client.entity.UrlEncodedFormEntity;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.impl.client.CloseableHttpClient;
Import org.apache.http.impl.client.HttpClients;
Import Org.apache.http.message.BasicNameValuePair;
Import Org.apache.http.util.EntityUtils;


public class Smscodeutil {
private static final String app_key= "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//NetEase cloud Letter assigned Account
private static final String app_secret= "xxxxxxxxxxxx";//Key assigned by NetEase Cloud
private static final String nonce= "000000";//random number

private static final String sendmsg_url= "https://api.netease.im/sms/sendcode.action";//request path URL to send verification code
private static final String checkcode_url= "https://api.netease.im/sms/verifycode.action";//Check the request path URL of the verification code

private static final char[] hex_digits={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '};
private static final String algorithm= "SHA";

Calculate and obtain a verification code
public static string Getverificationcode (String appsecret,string nonce,string curtime) {
String param = appsecret+nonce+curtime;
String Verificationcode = "";
try {
MessageDigest messagedigest=messagedigest.getinstance (algorithm);
Messagedigest.update (Param.getbytes ());
byte[] bytes = Messagedigest.digest ();

int len=bytes.length;
StringBuilder sb=new StringBuilder (len*2);
for (int i=0;i<len;i++) {
Sb.append (hex_digits[(bytes[i]>>4) &0x0f]);
Sb.append (hex_digits[bytes[i]&0x0f]);
}
Verificationcode = Sb.tostring ();
return verificationcode;
} catch (Exception e) {
throw new RuntimeException (e);
}
}

Send SMS to get generated verification code
public static string SendMessage (String phone) throws IOException {
Closeablehttpclient httpclient = Httpclients.createdefault ();
HttpPost post = new HttpPost (Sendmsg_url);

String curtime=string.valueof ((New Date (). GetTime ()/1000l));
String Checksum=smscodeutil.getverificationcode (app_secret,nonce,curtime);

Set the header of the request
Post.addheader ("AppKey", App_key);
Post.addheader ("Nonce", nonce);
Post.addheader ("Curtime", curtime);
Post.addheader ("CheckSum", CheckSum);
Post.addheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

Set Request parameters

List<basicnamevaluepair> namevaluepairs =new arraylist<basicnamevaluepair> ();
Basicnamevaluepair Basicnamevaluepair = new Basicnamevaluepair ("mobile", phone);
Namevaluepairs.add (Basicnamevaluepair);

Post.setentity (New Urlencodedformentity (Namevaluepairs, "utf-8"));

Execute request
HttpResponse Response=httpclient.execute (POST);
String responseentity= entityutils.tostring (response.getentity (), "utf-8");

To determine if the send was successful, send successful return True
String code = jsonobject.fromobject (responseentity). getString ("code");
if (Code.equals ("200")) {
Return "Success";
}
return "error";
}

Verify that the SMS Verification Code matches
public static string Checkverificationcode (String phone,string verificationcode) throws IOException {
Closeablehttpclient httpclient = Httpclients.createdefault ();
HttpPost post = new HttpPost (Checkcode_url);

String curtime=string.valueof ((New Date (). GetTime ()/1000l));
String Checksum=smscodeutil.getverificationcode (app_secret,nonce,curtime);

Set the header of the request
Post.addheader ("AppKey", App_key);
Post.addheader ("Nonce", nonce);
Post.addheader ("Curtime", curtime);
Post.addheader ("CheckSum", CheckSum);
Post.addheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

Set Request parameters
List<basicnamevaluepair> namevaluepairs =new arraylist<basicnamevaluepair> ();
Basicnamevaluepair mobilephone = new Basicnamevaluepair ("mobile", phone);
Namevaluepairs.add (Mobilephone);
Basicnamevaluepair Codeverificationcode = new Basicnamevaluepair ("code", Verificationcode);
Namevaluepairs.add (Codeverificationcode);

Post.setentity (New Urlencodedformentity (Namevaluepairs, "utf-8"));
Execute request
HttpResponse Response=httpclient.execute (POST);
String responseentity= entityutils.tostring (response.getentity (), "utf-8");

To determine if the send was successful, send successful return True
String code = jsonobject.fromobject (responseentity). getString ("code");
if (Code.equals ("200")) {
Return "Success";
}
return "error";
}

public static void Main (string[] args) throws IOException {

Test Getverificationcode () method
String appsecret= "xxxxxxxxxxxx";//Key assigned by NetEase Cloud Trust
String nonce= "000000";//random number
String curtime=string.valueof ((New Date (). GetTime ()/1000l));
String AA = Mobilemsgutil.getverificationcode (Appsecret, nonce, curtime);
SYSTEM.OUT.PRINTLN (AA);

Test SendMessage () method
String phone = "00000000000";
String code = mobilemsgutil.sendmessage (phone);
System.out.println ("Status of SMS Verification Code Acquisition" +code);

Test Checkverificationcode () method
String phone = "00000000000";
String verificationcode = "0000";
String code = Mobilemsgutil.checkverificationcode (Phone,verificationcode);
SYSTEM.OUT.PRINTLN ("SMS Verification code matches the status" +code);
}
}

SMS Verification (JAVA)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.