本文主要是介绍JAVA利用飞信接口发送短信_控控控-上ke控_新浪博客,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
飞信接口地址:http://w.ibtf.net/f.php
Fetion工具类:
package cpsa3.pack.util;
import java.io.IOException;
import org.apache.commons.httpclient.Header;
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;
public class Fetion {
public static void sendMsg(String _phone,String _pwd,String _to,String _msg) throws HttpException, IOException{
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://3.ibtf.sinaapp.com/f.php");
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在头文件中设置转码
NameValuePair[] data ={
new NameValuePair("phone", _phone),
new NameValuePair("pwd", _pwd),
new NameValuePair("to",_to),
new NameValuePair("msg",_msg),
new NameValuePair("type","0")
};
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("utf-8"));
//System.out.println(result);
System.out.println("ok!");
post.releaseConnection();
}
}
Test测试类:
package cpsa3.pack.util;
import java.io.IOException;
import org.apache.commons.httpclient.HttpException;
public class Test {
private static String PHONE = "152********";
private static String PWD = "********";
private static String TO = "152********";
private static String MSG = "你好:Hello World!";
public static void main(String[] args) throws HttpException, IOException {
Fetion.sendMsg(PHONE, PWD, TO, MSG);
}
}
Ps、需要导入3个Jar包
commons-codec-1.4.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
这篇关于JAVA利用飞信接口发送短信_控控控-上ke控_新浪博客的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!