您的位置:首页 > 编程语言 > Java开发

Java使用手机发送短信程序

2015-06-27 21:36 615 查看
手机短信

//需要的导入的包
import 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;

public class phone {
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", " 需要发送短信的内容") };
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();
}
}


该程序使用的是中国网建提供的接口。网址:http://sms.webchinese.cn/Login.shtml

需要下载的包,在官网有。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  发送短信