Java는 SMS SMS 플랫폼을 통해 SMS 메시지 기능을 구현합니다.
나는 프로젝트에서 문자 메시지를 보내는 기능을 사용했지만 내부 제한으로 인해 오늘 간단한 온라인을 찾아서 할 일이 없을 때 다음과 같이 기록했습니다.
이 프로그램은 China Net Construction에서 제공하는 SMS SMS 플랫폼을 사용하여 구현됩니다 (플랫폼은 현재 등록 된 사용자에게 5 개의 무료 SMS 및 3 개의 무료 MMS를 제공합니다. 이는 테스트하기에 충분합니다. 사용하기 전에 등록해야하며 등록 주소는 http://sms.webchinese.cn/reg.shtml)입니다. 다음은 프로그램 소스 코드입니다.
/ ** * @author dengsilinming * @date 2012-9-18 * */ package com.dengsilinming.mail; import java.io.ioexception; import org.apache.commons.httpclient.header; import org.apache.commons.httpclient.httpclient; import org.apache.commons.httpclient.httpclient; import org.apache.commons.httpclient.httpexception; import org.apache.commons.httpclient.namevaluepair; import org.apache.commons.httpclient.methods.postmethod; 공개 클래스 sendmsg_webchinese { / ** * @author dengsilinming * @date 18, 2012 년 9 월 18 일 * @Time 9:38:25 AM * @param args * @throws ioexception * @throws httpexception * @description * / public static void main (strows) httpexcement, ioexmence {htpection {htpection {httpcence httpclient (); postmethod post = new postMethod ( "http://gbk.sms.webchinese.cn"); // postMethod post = new postMethod ( "http://sms.webchinese.cn/web_api/"); post.addrequestheader ( "content-type", "application/x-www-form-urlencoded; charset = gbk"); // transcoding namevaluepair [] data = {new namevaluepair ( "uid", "dengsilinming"), // registered username new key "," "72da78da5ff54f450505"), // 성공적으로 등록한 후, 웹 사이트에 로그인 한 후 얻은 키는 새로운 이름의 NameValuePair ( "smsmob", "12345678900"), // 모바일 전화 번호 새로운 NameValuePair ( "smstext", "이 메시지 전용 메시지를 전하는 것입니다."). Post.SetRequestBody (데이터); client.executemethod (post); 헤더 [] 헤더 = post.getResponseHeaders (); int statusCode = post.getStatusCode (); System.out.println ( "상태 코드 :" + 상태 코드); for (헤더 h : 헤더) {system.out.println ( "---" + h.toString ()); } string result = new String (post.getResponseBodyAsString (). getBytes ( "gbk")); System.out.println (결과); }} 사용할 3 가지 항아리 패키지가 있습니다.
커먼즈-로깅 1.1.1.jar
Commons-httpclient-3.1.jar
Commons-Codec-1.4.jar
다음 내용은 China Jianshe SMS SMS API에서 발췌 한 내용입니다.
GBK 인코딩 인터페이스 주소 전송 :
http://gbk.sms.webchinese.cn/?uid=user이 사이트의 이름 & key = 인터페이스 보안 암호 및 smsmob = 모바일 번호 및 smstext = sms 컨텐츠
UTF-8 인코딩 인터페이스 주소 인코딩 :
http://utf8.sms.webchinese.cn/?uid=user이 사이트의 이름 및 key = 인터페이스 보안 암호 및 smsmob = mobile 번호 및 smstext = SMS 인터페이스 주소 (UTF8)를 얻으려면 SMS 컨텐츠 :
http://sms.webchinese.cn/web_api/sms/?
http://sms.webchinese.cn/web_api/sms/gbk/?action=sms_num&uid=username이 사이트의 key = 인터페이스 보안 비밀번호
팁 : HTTP가 URL 인터페이스를 호출하면 호출하기 전에 매개 변수 값을 인코딩해야합니다.
13888888886, 1388888887, 13888888888888과 같은 여러 휴대폰 번호를 분리하기 위해 반쪽의 반을 사용하여 한 번에 최대 50 개의 휴대 전화로 문자 메시지를 보내고 긴 문자 메시지를 지원합니다. 최대 300 단어, 일반 문자 메시지 70 단어/게시물 64 단어/게시물 청구
다음은 다른 언어로 SMS 인터페이스를 호출하는 간단한 데모입니다.
1. ASP 호출
<% '공통 함수'URL 대상 웹 페이지 주소를 입력하고 반환 값 GethttpPage는 대상 웹 페이지 기능 GethttpPage (url) dim http set http = server.createobject ( "msxml2.xmlhttp") http.open "get", url, if http. http.readystate <> 4 그런 다음 gethttppage = bytestobstr (http.responsebody, "gb2312") set http = nothing.number.number.number.number.number.number.number.number.clear end function bytestobstr (body, cset) dimjstream set objstream = server.createob ") objstream.type = 1 objstream.mode = 3 objstream.open objstream.write body objstream.position = 0 objstream.type = 2 objstream.charset = cset bytestobstr = objstream.readtext objstream.close set objstream.close set abjstream = onde end fortment '당신은 계정과 비밀번호를 추가했습니다. sms_url = "http://sms.webchinese.cn/web_api/?uid=account&key=interface key & smsmob = mobile number & smstext = sms content.write gethttppage (sms_url) %>
2.C# 전화
// system.net을 사용하는 데 필요한 네임 스페이스; System.io 사용; system.text 사용; // 호출 할 때 철자가있는 URL을 함수로 전달하면됩니다. 반환 값 공개 문자열 gethtmlfromurl (문자열 URL) {String strret = null; if (url == null || url.trim (). toString () == "") {return strret; } string targeturl = url.trim (). toString (); {httpwebrequest hr = (httpwebrequest) webrequest.create (targeturl); hr.useragent = "Mozilla/4.0 (호환 가능; MSIE 6.0; Windows NT 5.1)"; hr.method = "get"; HR.TimeOut = 30 * 60 * 1000; Webresponse hs = hr.getResponse (); 스트림 sr = hs.getResponsestream (); StreamReader ser = new StreamReader (sr, encoding.default); strret = ser.readtoend (); } catch (예외) {strret = null; } return strret; } 3. 자바 호출
java.io.unsupportedencodingException 가져 오기; 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; 공개 클래스 sendmsg_webchinese {public static void main (String [] args)은 예외 {httpclient client = new httpclient (); postmethod post = new postMethod ( "http://gbk.sms.webchinese.cn"); post.addrequestheader ( "content-type", "application/x-www-form-urlencoded; charset = gbk"); // transcoding namevaluepair [] data = {new namevaluepair ( "uid", "이 사이트의 사용자 이름"), 새로운 namevaluepair ( "key", "new", "new", "New" NameValuePair ( "Smsmob", "Mobile Number"), New NameValuePair ( "Smstext", "SMS Content")}; Post.SetRequestBody (데이터); client.executemethod (post); 헤더 [] 헤더 = post.getResponseHeaders (); int statusCode = post.getStatusCode (); System.out.println ( "상태 코드 :"+상태 코드); for (헤더 h : 헤더) {system.out.println (h.toString ()); } string result = new String (post.getResponseBodyAsString (). getBytes ( "gbk")); System.out.println (결과); post.releaseconnection (); }} JAR 패키지 다운로드
커먼즈-로깅 1.1.1.jar
Commons-httpclient-3.1.jar
Commons-Codec-1.4.jar
4. PHP 호출
$ url = 'http : //sms.webchinese.cn/web_api/? uid = ac echo get ($ url); 함수 get ($ url) {if (function_exists ( 'file_get_contents')) {$ file_contents = file_get_contents ($ url); } else {$ ch = curl_init (); $ 타임 아웃 = 5; curl_setopt ($ ch, curlopt_url, $ url); curl_setopt ($ ch, curlopt_returntransfer, 1); curl_setopt ($ ch, curlopt_connecttimeout, $ timeout); $ file_contents = curl_exec ($ ch); curl_close ($ ch); } return $ file_contents; } 5.VB.NET 호출
'SMS를 보내려면 전화 번호를 받으려면 nolist. 메모 내용의 여러 번, 별도 및 70 단어 사용
공개 함수는 sendsms (string으로, byval memo, string) string string as string = "http://sms.webchinese.cn/web_api/?uid=account&key=interface key & smsmob = mobile numb srcstring as string = webclient.downloadString (URL) 반환 srcstring catch return "-444"종료 시도 엔드 기능
테스트 후 위의 Java 소스 코드를 성공적으로 전송할 수 있지만 다른 언어에는 테스트가 없습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.