欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 飞信接口 博客分类: java 基础 java飞信 

程序员文章站 2024-03-04 18:25:12
...
本接口走的是移动wap飞信接口,绝对安全

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="手机号";//实际使用时可放在配置文件中
	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;";//发短信的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;		//对应的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是因为登录后不能马上发短信的缘故
	} 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("&amp", begin);
		return all.substring(begin,end);
	}
	/**
	 *@return String 放回的东西没什么意义只是看看移动到底返回什么给我
	 * @param touserid 是对方手机号
	 */
	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过期等无法发送的问题
			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 原来方法中是用的是post方法,但是好像post没用,不知是我代码问题还是移动问题,反正get方法有用,方法名我就没改
	 */

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("好友手机号");
		System.out.println("sfsdf");
		f.sendMsg("消息", "你的手机号");
	}

	
	
}

 

相关标签: java 飞信