MSN API编程例子

来源:互联网 发布:mysql联合主键文档 编辑:程序博客网 时间:2024/05/01 11:30

 先到 http://sourceforge.net/projects/jmsn/ 下载 lib 包msnm.jar

再到 sun 官方网站下载 jsse.jar 包

最后...

package stock;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import rath.msnm.MSNMessenger;
import rath.msnm.SwitchboardSession;
import rath.msnm.UserStatus;
import rath.msnm.event.MsnAdapter;
import rath.msnm.entity.*;
import rath.msnm.msg.*;

public class StockMsn {
 
public static String friend = "hunter_k_gd@hotmail.com";
 
public static String me = "hunter-hw@hotmail.com";
 
public static String password = "z123";

 
public static void main(String[] args) throws IOException,
   InterruptedException 
{
  MSNMessenger msn 
= new MSNMessenger(me, password);
  msn.setInitialStatus(UserStatus.ONLINE);
  
// 监听
  msn.addMsnListener(new MsnAdapter() {
   
public void progressTyping(SwitchboardSession ss, MsnFriend friend,
     String typingUser) 
{
    System.out.println(
"Typing on " + friend.getLoginName());
   }

   
public void instantMessageReceived(SwitchboardSession ss,
     MsnFriend friend, MimeMessage mime) 
{
    System.out.println(
"*** MimeMessage from "
      
+ friend.getLoginName());
    System.out.println(mime.getMessage());
    System.out.println(
"*****************************");
   }

  }
);

  
// 登陆
  System.out.println(" msn login....");
  msn.login();

  
// 呼叫
  msn.doCall(friend);
  Thread.sleep(
5000);// 延迟,SwitchboardSession创建容易失败
  SwitchboardSession ss = null;
  
while (ss == null{
   System.out.println(
" Tring call " + friend);
   ss 
= msn.doCallWait(friend);
   Thread.sleep(
1000);// 循环,直到创建SwitchboardSession成功
  }


  System.out.println(
" SwitchboardSession =" + ss + ",创建成功!可以聊天了...");

  
// 保持聊天
  boolean exit = false;
  
while (!exit) {
   DataInputStream stdio 
= new DataInputStream(
     
new BufferedInputStream(System.in));
   String line 
= "请输入信息: ";
   System.out.println(line);
   
while ((line = stdio.readLine()).length() != 0{
    
// 这里转换字符集
    byte[] tmpbyte = line.getBytes("ISO8859_1");
    line 
= new String(tmpbyte);

    System.out.println(
"Sending to" + friend + " : " + line);

    MimeMessage msg 
= new MimeMessage();
    msg.setKind(MimeMessage.KIND_MESSAGE);
    msg.setMessage(line);
    
while (ss == null{
     System.out.println(
" Retring call " + friend);
     ss 
= msn.doCallWait(friend);
     Thread.sleep(
1000);// 循环,直到创建SwitchboardSession成功
    }

    ss.sendInstantMessage(msg);

   }

  }


 }

}