我的雲端生活網 - Life+

Monday, November 10, 2008

3分鐘學會使用程式發送msn訊息, Java篇(三)

<imoo msn機器人測試平台,將於下週三(2009.6.24)取消所有未經申請試用的認證資訊,詳見相關資訊>



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訊息控制開發套件程式介面說明書

7 comments:

Willy said...

好實用的文章
謝謝啦

te said...

感謝你的指教

也歡迎您使用這個工具撰寫相關程序後分享給大家,如果需要個別的帳號來測試/使用這個這個工具,可以跟我聯繫SONET.ALL@GMAIL.COM,我們會提供您特別的獨立帳號

蛋泥。等你。 said...

你好:
我依您上面的範例,可是跑到
GETSPID()最後在tmp會得到
"0 錯誤的認證資訊 "
可是我的帳密都沒有錯
(帳號嘗試過XXX@yahoo.com.tw 和直接打XXX都無效)
請問這樣有可能是哪裡的問題

謝謝~

te said...

抱歉! 我並不熟析java , 不過範例寫的應該很清楚,要取得spid 的帳號/密碼是

String UserName = "apiblogt3";
String Password = "msnsdkt";

取得spid 後,使用這個key 才能做註冊聯絡人...function

您可以參考直接使用瀏覽器發送訊息的流程
http://rd-program.blogspot.com/2008/12/msn_18.html

完整 msnSDK簡介與文件下載
http://rd-program.blogspot.com/2008/10/msnsdk.html

如有進一步的合作可洽 service@imoo.tw 或sonet.all@gmail.com

te said...

另外;我查了一下您所交易的log,發現一個問題;你不能註冊不存在的使用者(REGISTER)
xxxxx@hotmail.com , 這在範例的註解有提到,所謂的(REGISTER),指的是您要機器人加入的聯絡人(機器人會主動邀請對方加入),之後您才能發送訊息,而當您(REGISTER dereky_peng@yahoo.com.tw) 時請著注意,這代表你要把它加入YAHOO即時通,當您註冊YAHOO 的帳號為MSN聯絡人時,應該使用(REGISTER_ATMSN),這在文件上有提到

楊忠翰 said...

我想請問一下這程式只要完整的COPY 過去 然後把聯絡人那邊改一下因該就能使用了對吧? 可是我做了很多次 她都會出現 連線超時
是因為連結關掉了嗎?? 而且 直接用瀏覽器發送訊息的 也不能用了

te said...

文章上應有標註這份說明

為了提供更即時的測試諮詢服務,imoo msn機器人測試平台,將於下週三(2009.6.24)取消所有未經申請試用的認證資訊,改由申請制的方式辦理測試,相關測試申請可直接發信至service@imoo.tw 洽詢

Thanks

Blog Archive