Rumah > Soal Jawab > teks badan
高洛峰2017-04-17 17:34:33
这个你要自己写吗? 我建议你直接调用短信平台的接口不就可以了吗?
短信发送
//接口地址
String url = "http://183.203.28.5:9000/HttpSmsMt";
//下发时间
String mttime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
Map<String, String> param = new HashMap<String, String>();
param.put("name", "用户帐号");
param.put("pwd", Tools.MD5("用户密码"+mttime));
param.put("content", URLEncoder.encode("【阅信短信验证码】验证码888888,打死也不能告诉别人哦。", "UTF-8"));
param.put("phone", "13400000000");
param.put("subid", "");
param.put("mttime", mttime);
HttpTool.sendPost(url, param);
POST提交方法
public static String sendPost(String url, Map<String, String> params) {
Log.i("POST提交:[url="+url+"]"+params.toString());
URL u = null;
HttpURLConnection con = null;
// 构建请求参数
StringBuffer sb = new StringBuffer();
if (params != null) {
for (Entry<String, String> e : params.entrySet()) {
sb.append(e.getKey()).append("=").append(e.getValue()).append("&");
}
sb.substring(0, sb.length() - 1);
}
// 尝试发送请求
try {
u = new URL(url);
con = (HttpURLConnection) u.openConnection();
con.setRequestMethod("POST");
con.setConnectTimeout(6000);
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
osw.write(sb.toString());
osw.flush();
osw.close();
} catch (Exception e) {
Log.e(e);
} finally {
if (con != null) {
con.disconnect();
}
}
// 读取返回内容
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String temp;
while ((temp = br.readLine()) != null) {
buffer.append(temp).append("n");
}
} catch (Exception e) {
Log.e(e);
}
Log.i("POST响应:"+buffer.toString());
return buffer.toString();
}
MD5加密方法
public static String MD5(String str){
MessageDigest md5 = null;
try{
md5 = MessageDigest.getInstance("MD5");
}catch (Exception e){
Log.i(e.getMessage());
return "";
}
char[] charArray = str.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++){
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16){
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}