一、代碼示例
package com.aaa.zxf.login; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * 一、登錄這個網站,注冊登錄。 http://www.ihuyi.com/product.php 可以免費使用10次。 * * *二、找到他的代碼示例。在下面會有圖片說明 * * *三、根據自己的需要,可以對示例代碼進行修改。下面的代碼是來自於對 https://blog.csdn.net/qq_17025903/article/details/73331091 的編輯。 * * *四、jar包管理。 * 1. 點擊他的接口下載,里面提供的有jar包。 * 2.maven項目需要直接去搜索 * * <dependencies> * <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> * <dependency> * <groupId>commons-codec</groupId> * <artifactId>commons-codec</artifactId> * <version>1.3</version> * </dependency> * * * <!-- https://mvnrepository.com/artifact/dom4j/dom4j --> * <dependency> * <groupId>dom4j</groupId> * <artifactId>dom4j</artifactId> * <version>1.6.1</version> * </dependency> * * * * <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> * <dependency> * <groupId>org.apache.httpcomponents</groupId> * <artifactId>httpclient</artifactId> * <version>4.5.6</version> * </dependency> * * <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> * <dependency> * <groupId>commons-httpclient</groupId> * <artifactId>commons-httpclient</artifactId> * <version>3.0-rc4</version> 我的這個jar包導不進來,maven項目沒有搞定。 * </dependency> * * */ public class SendsmsDemo { private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit"; //直接將示例代碼中的URL 拿過來用就行。 public static int sendSms(String phone) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(Url); client.getParams().setContentCharset("GBK"); method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=GBK"); int mobile_code = (int) ((Math.random() * 9 + 1) * 100000); String content = new String("您的驗證碼是:" + mobile_code + "。請不要把驗證碼泄露給其他人。"); //提交短信 NameValuePair[] data = { // name 和 password 需要自行修改。 new NameValuePair("account", "C51843367"), //查看用戶名 是登錄用戶中心->驗證碼短信->產品總覽->APIID new NameValuePair("password", "4839f1a37f6e5240109e6bf1cc8fd647"), //查看密碼請登錄用戶中心->驗證碼短信->產品總覽->APIKEY new NameValuePair("mobile", phone), new NameValuePair("content", content), }; method.setRequestBody(data); try { client.executeMethod(method); String SubmitResult = method.getResponseBodyAsString(); System.out.println(SubmitResult); Document doc = DocumentHelper.parseText(SubmitResult); Element root = doc.getRootElement(); String code = root.elementText("code"); String msg = root.elementText("msg"); String smsid = root.elementText("smsid"); System.out.println(code); System.out.println(msg); System.out.println(smsid); if ("2".equals(code)) { System.out.println("短信提交成功"); return mobile_code; //驗證碼 } else return 0; } catch (Exception e) { e.printStackTrace(); return 0; } } //測試 手機號 public static void main(String[] args) { sendSms("15290859821"); } }
二、圖片說明