首先创建要给Maven工程,因为需要引入阿里云为短信服务提供的sdk,
pom.xml如下:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>4.11.0</version>
</dependency>
下面的可以不要,因为sdk依赖的java版本较高,所以,我引入了下面的
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
接下来就是简单的写代码过程啦:
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
public class SendSms {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("default", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.GET);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "default");
request.putQueryParameter("PhoneNumbers", "<手机号>");
request.putQueryParameter("SignName", "SignName");
request.putQueryParameter("TemplateCode", "SMS_172170657");
request.putQueryParameter("TemplateParam", "{\"code\":\"this0is0a0test\"}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ClientException e) {
e.printStackTrace();
}
}
}
上面的accessKeyId
,accessSecret
和SignName
都是要购买该服务获取的,TemplateCode
是要申请的,其他的我们可以根据情况设置。
上面的代码是一个发短信的案例,
下面我们来看一个查询案例:
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
*/
public class QuerySendDetails {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("default", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("QuerySendDetails");
request.putQueryParameter("RegionId", "default");
request.putQueryParameter("PhoneNumber", <手机号>);
request.putQueryParameter("SendDate", "20190809");
request.putQueryParameter("PageSize", "10");
request.putQueryParameter("CurrentPage", "1");
// request.putQueryParameter("BizId", "555");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
代码非常相识的,他们的动作参数不同而已。
网友评论