Java calls WebService SMS interface

1. WebService interface description


It is sent in the form of http post, the above is the request information, and the following is the return value.


The return value section of the interface documentation is described.


2. Java code, please refer to my other articles for the MD5 tool class, or find an MD5 encryption tool class by yourself. The final MD5 result is 32 as uppercase.

public class SendSMS {

	//SMS interface address
	private static String Url = "http://sdk.entinfo.cn:8061/webservice.asmx/mdsmssend";

	public static Integer  send(String mobile) {
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);

		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");

		int mobile_code = (int)((Math.random()*9+1)*100000);
		String content = new String("Your verification code is: " + mobile_code + ". Please do not disclose the verification code to others.");

		NameValuePair[] data = {//Package parameters
				new NameValuePair("sn", "your own sn"),
				// The password can be either a plaintext password or encrypted using 32-bit MD5
				new NameValuePair("pwd", MD5.md5("your own sn and pwd").toUpperCase()),
				new NameValuePair("mobile", mobile),
				new NameValuePair("content", content),
				new NameValuePair("ext", ""),
				new NameValuePair("stime", ""),
				new NameValuePair("rrid", ""),
				new NameValuePair("msgfmt", "")
		};

		method.setRequestBody(data);

		try {
			client.executeMethod(method);
			//Get the returned Xml
			String SubmitResult =method.getResponseBodyAsString();

			//parse Xml
			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement ();

			Long returnValue = Long.parseLong(root.getText());
			//Comparing with the return value, if the information is negative, there is an error, and >0 indicates that the transmission is successful (refer to the returned message for analysis)
			//Return the generated verification code and the user input for verification
			if(returnValue > 0){
				return mobile_code;
			}else {
				return null;
			}

		} catch (HttpException e) {
			e.printStackTrace ();
			return null;
		} catch (IOException e) {
			e.printStackTrace ();
			return null;
		} catch (DocumentException e) {
			e.printStackTrace ();
			return null;
		} catch (NumberFormatException e) {
			e.printStackTrace ();
			return null;
		}
	}
}

3. Forget it, I'll post one of the MD5 tools.

import java.security.MessageDigest;

public class MD5 {

	public static String md5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (Exception e) {
			e.printStackTrace ();

		}
		return str;
	}
	public static void main(String[] args) {
		System.out.println(md5("sn"+"pwd"));
	}
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325548360&siteId=291194637