短信平台会给一份接口文档,参照接口文档参数要求来做即可。
以下都是post请求
案例代码1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class MessagePlatfromDemo {
public static void main(String[] args) {
String platformUrl = "xxxx";
//声明List集合,用来封装表单中的参数
List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
paramsList.add(new BasicNameValuePair("参数", "值"));
paramsList.add(new BasicNameValuePair("参数2", "值2"));
paramsList.add(new BasicNameValuePair("参数3", "值3"));
sendPostRequest(platformUrl,paramsList);
}
public static String sendPostRequest(String url, List<NameValuePair> params) {
String result = "";
// 创建httpClient对象
HttpClient httpClient = HttpClients.createDefault();
// 创建httpPost
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = null;
// 执行请求
try {
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = entity.getContent();
result = getExecuteStatus(inputStream);
}
} catch (IOException e) {
e.printStackTrace();
}
;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
public static String getExecuteStatus(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
try {
while ((line = reader.readLine()) != null) {
/**
* 处理返回结果内容 可能是xml格式或者其他
*/
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回能判断成功的值
return null;
}
}
案例代码2
package com.liangxun.common;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class Message
{
/**
* 通过URL地址的形式发送短信
* @param mobileNum
* @param content
* @return
*/
public static String sendMobileMessageByURL(String mobileNum,String content) {
String state = "0";
try {
URL url = new URL("http://xxxxxx");//短信URL地址
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
String MobileMessageInfo = "参数1=111111&参数2="+MessageMD5.MD5("111111")+"&PHONE="+mobileNum+"&CONTENT="+content;
out.write(MobileMessageInfo);
out.flush();
out.close();
DataInputStream in = new DataInputStream(connection.getInputStream());
state = in.readLine();//1为短信发送成功,否则短信发送失败
in.close();
}
catch (IOException e) {
System.out.println(e);
}
return state;
}
public static void main(String args[]){
sendMobileMessageByURL("156511xx907","您的验证码:123456【时代】");
}
}
package com.liangxun.common;
import java.security.MessageDigest;
public class MessageMD5 {
public final static String MD5(String s) {
char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd','e', 'f'};
try {
byte[] strTemp = s.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexdigits[byte0 >>> 4 & 0xf];
str[k++] = hexdigits[byte0 & 0xf];
}
return new String(str);
}
catch (Exception e) {
return null;
}
}
}