package com.example.demo.testng;
import org.testng.annotations.Test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
public class TestngMain {
public static String getMD5Str(String inStr) {
System.out.println(inStr);
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.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();
}
public static String sendPostRequest(URL url, String CharSet, String postData)
throws Throwable {
System.out.println(url+"?"+postData);
HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setConnectTimeout(6000);
con.setReadTimeout(6000);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("contentType",CharSet);
OutputStream os = con.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.write(postData.getBytes());
dos.flush();
dos.close();
InputStream in = con.getInputStream();
BufferedReader dis = new BufferedReader(new InputStreamReader(in,CharSet));
String data = "", line = "";
while ((line = dis.readLine()) != null) {
data += line;
}
// data = new String(data.getBytes(CharSet),"utf-8");
if (in != null) {
in.close();
}
if (dis != null) {
dis.close();
}
if (con != null) {
con.disconnect();
}
return data;
}
public static void main(String[] args) throws Throwable {
Long sendtime = System.currentTimeMillis();
String content = "{\"code\":\"88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888810011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001\"}";
String toAddress= "18288888888";
String type="1059";
String appId="2015878";
String appKey="79a245e041b8d6007d612eae03c1akdind";
String token = getMD5Str(java.net.URLEncoder.encode(content+toAddress+type+appId+appKey+sendtime+"syswintoon", "utf-8")).toLowerCase();
String urlstr= "http://192.168.235.55/sendsms.do";
try {
URL url = new URL(urlstr);
System.out.println("token : "+token);
System.out.println("sendtime : "+sendtime);
System.out.println(sendPostRequest(url,"utf-8","content="+content+"&toAddress="+toAddress+"&type="+type+"&appId="+appId+"&sendTime="+sendtime+"&token="+token+"&retry=0"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
原文:http://blog.51cto.com/357712148/2154273