跳到主要內容

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

留言

Willy寫道…
好實用的文章
謝謝啦
薛共和寫道…
感謝你的指教

也歡迎您使用這個工具撰寫相關程序後分享給大家,如果需要個別的帳號來測試/使用這個這個工具,可以跟我聯繫SONET.ALL@GMAIL.COM,我們會提供您特別的獨立帳號
你好:
我依您上面的範例,可是跑到
GETSPID()最後在tmp會得到
"0 錯誤的認證資訊 "
可是我的帳密都沒有錯
(帳號嘗試過XXX@yahoo.com.tw 和直接打XXX都無效)
請問這樣有可能是哪裡的問題

謝謝~
薛共和寫道…
抱歉! 我並不熟析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
薛共和寫道…
另外;我查了一下您所交易的log,發現一個問題;你不能註冊不存在的使用者(REGISTER)
xxxxx@hotmail.com , 這在範例的註解有提到,所謂的(REGISTER),指的是您要機器人加入的聯絡人(機器人會主動邀請對方加入),之後您才能發送訊息,而當您(REGISTER dereky_peng@yahoo.com.tw) 時請著注意,這代表你要把它加入YAHOO即時通,當您註冊YAHOO 的帳號為MSN聯絡人時,應該使用(REGISTER_ATMSN),這在文件上有提到
楊忠翰寫道…
我想請問一下這程式只要完整的COPY 過去 然後把聯絡人那邊改一下因該就能使用了對吧? 可是我做了很多次 她都會出現 連線超時
是因為連結關掉了嗎?? 而且 直接用瀏覽器發送訊息的 也不能用了
薛共和寫道…
文章上應有標註這份說明

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

Thanks

這個網誌中的熱門文章

怎麼在網路上註冊成為youbike 會員?

新版官網請參考  怎麼在網路上註冊成為youbike 會員?   http://rd-program.blogspot.tw/2014/04/youbike.html 網路的申請步驟類似,下面將以網路申請來說明申請步驟:申請的時候需要準備悠遊卡、或晶片信用卡,以及手機門號。 1. 請先登入ubike網址: http://www.youbike.com.tw/ ,登入後選擇【正體中文】,要選英文也可以啦! 2. 在螢幕的右上角選擇【註冊】。 3.點擊【開始註冊】。 4. 點擊【同意】。(沒有其他選擇?) 5. 輸入您的【手機號碼】以及【認證碼】,然後按【送出】。這時候手機會收到ubike傳來的簡訊,通之驗證碼,有四個阿拉伯數字。 6. 輸入帳號(手機號碼)、驗證碼(ubike傳到手機的簡訊)、密碼,然後按【下一步】。 7. 還沒完成喔!這裡告訴你如何租車及還車的步驟。把螢幕拉到最下面,記得勾選【我已清楚瞭解租還車步驟】,然後按【下一步】。 8. 選擇悠遊卡或是晶片信用卡,然後輸入卡片號碼,卡片號碼請參卡畫面又下方的提示位置,請注意有些卡號可能已經模糊不清,可能無法輸入。每隻手機不只可以輸入一個卡號。 9. 填寫個人姓名及Email帳號,如果不想收到相關訊息就把前面的打勾取消。按【確認】按鈕。 10. 恭喜您註冊成功,可已開始使用YouBike了。

Youbike 線上註冊會員

Youbike 線上註冊會員 https://www.youbike.com.tw/cht/f61.html youbike sitemap   網站內容快速連結  場站地圖 場站列表 服務中心資訊 預計設站地點 媒體報導 活動情報 緊急通知 營運資訊 營運成果 關於YouBike 大事記 設備介紹 租還方式 費率說明 安全騎乘 分類列表 失物招領 YouBike協尋 加入會員 會員資料管理 YouBike電子報 騎乘記錄查詢 合作夥伴 友站連結 一般文件 APP 隱私權政策 連絡我們