天天看点

JAVA如何实现发送短信

浠婂ぉ鐪嬪埌浜嗕竴涓渶姹傦紝闇€瑕佸仛涓€涓彂閫佺煭淇$殑鍔熻兘锛岀綉涓婃壘浜嗘壘锛屽ぇ姒傛湁杩欏嚑绉嶆柟娉曪細(1)浣跨敤webservice鎺ュ彛鍙戦€佹墜鏈虹煭淇?杩欎釜鍙互浣跨敤sina鎻愪緵鐨剋ebservice杩涜鍙戦€?浣嗘槸闇€瑕佽繘琛屾敞鍐?(2)浣跨敤鐭俊mao鐨勬柟寮忚繘琛岀煭淇$殑鍙戦€?杩欑鏂瑰紡搴旇鏄瘮杈冪殑甯哥敤,鍓嶆彁鏄渶瑕佽喘涔扮‖浠惰澶?鍛靛懙(3)浣跨敤涓浗缃戝缓鎻愪緵鐨凷MS鐭俊骞冲彴锛堢敵璇疯处鍙峰湴鍧€锛歨ttp://sms.webchinese.com.cn/锛夛紝鍚浜嗙綉寤烘彁渚涗簡鍑犳潯鍏嶈垂鐨勭煭淇★紝灏变笂鍘昏瘯浜嗚瘯锛岃皟鐢ㄤ簡涓嬩粬鐨勬帴鍙o紙瀹炰负璋冪敤鎺ュ彛锛屽叾瀹炲氨鏄嫹璐濅簡涓€娈典唬鐮侊紝鍛靛懙~锛夛紝浠g爜濡備笅锛屽拰澶у鍒嗕韩涓€涓嬶細

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class SendMsg_webchinese {
    public static void main(String[] args) throws Exception {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
    post.addRequestHeader("Content-Type",
                "application/x-www-form-urlencoded;charset=gbk");// 鍦ㄥご鏂囦欢涓缃浆鐮?        NameValuePair[] data = { new NameValuePair("Uid", "鏈珯鐢ㄦ埛鍚?),
                new NameValuePair("Key", "鎺ュ彛瀹夊叏绉橀挜"),
                new NameValuePair("smsMob", "鎵嬫満鍙风爜"),
                new NameValuePair("smsText", "楠岃瘉鐮侊細8888") };
        post.setRequestBody(data);

        client.executeMethod(post);
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        System.out.println("statusCode:" + statusCode);
        for (Header h : headers) {
            System.out.println(h.toString());
        }
        String result = new String(post.getResponseBodyAsString().getBytes(
                "gbk"));
        System.out.println(result); // 鎵撳嵃杩斿洖娑堟伅鐘舵€?
        post.releaseConnection();

    }
           

鎵€闇€jar鍖?

commons-codec-1.4

commons-httpclient-3.1.jar

commons-logging-1.1.1.jar