public class LinkSMS {
public static String HttpClientTest(String mobile,String content) {
// 接口账户名
String CorpID = "";
// 接口密码
String Pwd = "";
String result="";
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
AuthCache authCache = new BasicAuthCache();
HttpClientContext context = HttpClientContext.create();
context.setAuthCache (authCache);
//2.封装短信接口参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("CorpID",CorpID));
params.add(new BasicNameValuePair("Pwd",Pwd));
params.add(new BasicNameValuePair("Mobile", mobile));
params.add(new BasicNameValuePair("Content", content));
params.add(new BasicNameValuePair("Cell", ""));
params.add(new BasicNameValuePair("SendTime", ""));
HttpEntity entity = new UrlEncodedFormEntity(params,"GBK");
//3.提交访问地址
HttpPost request = new HttpPost ("https://sdk2.028lk.com");
request.setEntity(entity);
//4.提交访问短信接口
CloseableHttpResponse response = httpclient.execute(request);
//5.获取返回值并解析
HttpEntity entity2 = response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity2.getContent(), "GBK"));
String line="";
while ((line = br.readLine()) != null) {
result+= line+"\n";
}
//4.关闭链接
br.close();
response.close ();
httpclient.close ();
} catch (Exception e) {
e.getLocalizedMessage();
}
return result;
}
/**
*main测试
*/
public static void main(String[] args) {
String content="您的验证码为:233112,有效时间5分钟";
String phone="18797512522";
String result=LinkSMS.HttpClientTest(phone,content);
System.out.println("----------------接口提交返回值-------------------");
System.out.println(phone);
System.out.println(content);
System.out.print(result);
System.out.println("----------------------------------------------");
//对返回值的判断。。。此处省略
if(result!=null){
System.out.println("正确");
}else {
System.out.println("错误");
}
}
}