(msn机器人)3分钟学会使用程式发送msn讯息, Java篇(三)

3分钟学会使用程式发送msn讯息, Java篇(三)
http://rd-program.blogspot.com/2008/11/3msn-java.html
以下内容也可参考 http://rd-program.blogspot.com/2008/11/3msn-java.html

msnSDK讯息控制开发套件 同时支援MSN/Yahoo即时通 讯息双向传递
ps.也请参考 msnSDK的使用流程

public class clsMsnSDK {
public static void main(String[] args) throws Exception {
String UserName = "apiblogt3";
String Password = "msnsdkt";
//取得SPID
String strRet = GETSPID(UserName,Password);
String[]tmp = strRet.split("1\t");
String strValue = tmp[1].replace('\n',' ').trim();

String SPID = strValue;
System.out.println(SPID);
//注册联络人
String Email = "xxxxx@hotmail.com"; //填入msn/yahoo联络人,勿使用这个预设值
String ret = Register(Email,SPID);
System.out.println(ret);
//发讯息
String strMsg = "Hello_World!"; //输入讯息 (未做 Url EnCoded)
String MsgRet = SendMsg(Email,strMsg,SPID);
System.out.println(MsgRet);
}

//取得SPID
public static String GETSPID(String UserName,String Password) throws Exception {

String strUrl = "http://59.120.234.84:8082/msnSDK/msn_cgi-win32";
String strValue ="?FUNC=GETSPID&USERID=" + UserName + "&PASSWD=" + Password;
java.net.HttpURLConnection urlConnection= null;//定义一个HTTP连线
java.net.URL url= new java.net.URL(strUrl + strValue);//定义一串URL
urlConnection=(java.net.HttpURLConnection)url.openConnection();//将URL餵给HttpURLConnection
urlConnection.setRequestMethod("POST");//设定参数传递方式(GET or POST)
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();//开启连线
java.io.InputStream GetHTML=urlConnection.getInputStream();//内容储存下来
int leng =0;
byte[] Data = new byte[100];
byte[] totalData = new byte[0];
int totalLeg =0;
do{
leng = GetHTML.read(Data);//(Data);
if(leng>0){
totalLeg += leng;
byte[] temp = new byte[totalLeg];
System.arraycopy(totalData, 0, temp, 0, totalData.length);
System.arraycopy(Data, 0, temp, totalData.length, leng);
totalData = temp;
}
}while(leng>0);
String tmp = new String(totalData,"UTF-8");
urlConnection.disconnect();
return tmp;
}

//注册
public static String Register(String UIDS,String SPID) throws Exception{
String strUrl = "http://59.120.234.84:8082/msnSDK/msn_cgi-win32";
String strValue = "?FUNC=REGISTER&UIDS=" + UIDS + "&SESSION="+ SPID;
java.net.HttpURLConnection urlConnection= null;//定义一个HTTP连线
java.net.URL url= new java.net.URL(strUrl + strValue);//定义一串URL
urlConnection=(java.net.HttpURLConnection)url.openConnection();//将URL餵给HttpURLConnection
urlConnection.setRequestMethod("POST");//设定参数传递方式(GET or POST)
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();//开启连线
java.io.InputStream GetHTML=urlConnection.getInputStream();//内容储存下来
int leng =0;
byte[] Data = new byte[100];
byte[] totalData = new byte[0];
int totalLeg =0;
do{
leng = GetHTML.read(Data);//(Data);
if(leng>0){
totalLeg += leng;
byte[] temp = new byte[totalLeg];
System.arraycopy(totalData, 0, temp, 0, totalData.length);
System.arraycopy(Data, 0, temp, totalData.length, leng);
totalData = temp;
}
}while(leng>0);
String tmp = new String(totalData,"UTF-8");
urlConnection.disconnect();
return tmp;

}

//传送MSN 讯息
public static String SendMsg(String UIDS,String MSG,String SPID) throws Exception{
String strUrl = "http://59.120.234.84:8082/msnSDK/msn_cgi-win32";
String strValue ="?FUNC=SENDMSG&UIDS=" + UIDS + "&MSG=" + MSG +"&Encoding=BIG5&flag=0&Session=" + SPID;
java.net.HttpURLConnection urlConnection= null;//定义一个HTTP连线
java.net.URL url= new java.net.URL(strUrl + strValue);//定义一串URL
urlConnection=(java.net.HttpURLConnection)url.openConnection();//将URL餵给HttpURLConnection
urlConnection.setRequestMethod("POST");//设定参数传递方式(GET or POST)
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();//开启连线
java.io.InputStream GetHTML=urlConnection.getInputStream();//内容储存下来
int leng =0;
byte[] Data = new byte[100];
byte[] totalData = new byte[0];
int totalLeg =0;
do{
leng = GetHTML.read(Data);//(Data);
if(leng>0){
totalLeg += leng;
byte[] temp = new byte[totalLeg];
System.arraycopy(totalData, 0, temp, 0, totalData.length);
System.arraycopy(Data, 0, temp, totalData.length, leng);
totalData = temp;
}
}while(leng>0);

String tmp = new String(totalData,"UTF-8");
urlConnection.disconnect();
return tmp;
}

}

其他更详细的参数可以参考msnSDK讯息控制开发套件程式介面说明书


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章