java 飞信接口_java 飞信接口
本接口走的是移動(dòng)wap飛信接口,絕對(duì)安全
package net.duohuo.tengzhinei.Feition;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
public class Feition {
static final String MOBILE="手機(jī)號(hào)";//實(shí)際使用時(shí)可放在配置文件中
static final String PASSWORD="密碼啊";
static final String LOGINURL="http://f.10086.cn/im/login/inputpasssubmit1.action?";//登陸的URL
static final String SENDMSGURL="http://f.10086.cn/im/chat/sendShortMsg.action;";//發(fā)短信的URL
static final String SEARCH="http://f.10086.cn/im/index/searchOtherInfoList.action;";
static final String ADDFRIEND="http://f.10086.cn/im/user/insertfriend2.action;";
static final String CARD="jsessionid";
private String session;//對(duì)應(yīng)的session 這里是jsessionid=abcukqLZwma_35eswdRdt的形式
private String t;//t不知到是什么返回的連接中都包含他,且是變化的這里是t=26181745728233079的形式
public String getSession() {
return session;
}
public void setSession(String session) {
this.session = session;
}
public String getT() {
return t;
}
public void setT(String t) {
this.t = t;
}
/**
* @param args
*/
public boolean login(String mobile ,String password){//登錄
String tAndSession=null;
String loginMsg="m="+mobile+"&pass="+password+"&loginstatus=4";
String loginURL=LOGINURL+loginMsg;
String all=null;
try {
all=post(loginURL);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(all);
int begin=all.indexOf(CARD);
int end=all.indexOf("\"", begin);
tAndSession=all.substring(begin, end);
String s=tAndSession.substring(0,tAndSession.indexOf("?"));
String t=tAndSession.substring(tAndSession.indexOf("?")+1);
setSession(s);
try {
Thread.sleep(1000); //sleep是因?yàn)榈卿浐蟛荒荞R上發(fā)短信的緣故
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setT(t);
return true;
}
String getToUserid(String toPhone){
String url=SEARCH+getSession()+"?"+getT()+"&searchText="+toPhone;
String all = "";
try {
all=post(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int begin=all.indexOf("touserid=");
int end=all.indexOf("&", begin);
return all.substring(begin,end);
}
/**
*@return String 放回的東西沒(méi)什么意義只是看看移動(dòng)到底返回什么給我
* @param touserid 是對(duì)方手機(jī)號(hào)
*/
public String sendMsg(String msg,String toPhone){
if( getSession()==null||getT()==null){
login(MOBILE, PASSWORD);
}
try {
msg=URLEncoder.encode(msg, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String msgSendURL=SENDMSGURL+getSession()+"?"+getToUserid(toPhone)+"&"+getT()+"&msg="+msg;
String all=null;
try {
all=post(msgSendURL);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!all.contains("成功")){ //防止session過(guò)期等無(wú)法發(fā)送的問(wèn)題
login(MOBILE, PASSWORD);
sendMsg(msgSendURL, toPhone);
}
return all;
}
void addFrident(String phone){
login(MOBILE, PASSWORD);
String url=ADDFRIEND+getSession()+"?"+getT()+"&number="+phone+"&type=0";
String all=null;
try {
all=post(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(all);
}
/**
* @see 原來(lái)方法中是用的是post方法,但是好像post沒(méi)用,不知是我代碼問(wèn)題還是移動(dòng)問(wèn)題,反正get方法有用,方法名我就沒(méi)改
*/
public String post(String urlStr) throws IOException{
String result="";
URL url=new URL(urlStr);
HttpURLConnection connection= (HttpURLConnection)url.openConnection();
BufferedReader in=new BufferedReader(new InputStreamReader( connection.getInputStream(),"UTF-8"));
String line;
while((line=in.readLine())!=null){
result+=line;
}
return result;
}
public static void main(String[] args) throws IOException {
Feition f=new Feition();
f.addFrident("好友手機(jī)號(hào)");
System.out.println("sfsdf");
f.sendMsg("消息", "你的手機(jī)號(hào)");
}
}
分享到:
2011-07-13 11:41
瀏覽 1842
評(píng)論
1 樓
dingchuan
2012-08-19
有驗(yàn)證碼,連接失敗
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的java 飞信接口_java 飞信接口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python上下文管理关键字_[宜配屋]
- 下一篇: java注销登录_java实现注销登录