功能说明:
qq在线就在页面上输出【在线】的字样,qq不在线就输出【不在线】的字样
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.IO; using System.Text; namespace qqIsonline { public partial class index : System.Web.UI.Page { public string sendURL; protected void Page_Load(object sender, EventArgs e) { string qqnum = "2669743225"; string result = GetQQStatue(qqnum); if (result == "1") { Response.Write("在线"); } else { Response.Write("不在线"); } } /// <summary> /// 获得qq是否在线的状态 /// <param name="qqnum">要判断的q号码</param> /// <returns>online[0]=X的字符串,x=1或者x=0</returns> public string GetURL(string qqnum) { string resultStr; try { sendURL = "http://webpresence.qq.com/getonline?Type=1&" + qqnum + ":"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sendURL); //创建一个HttpWebRequest对象 HttpWebResponse respone = (HttpWebResponse)request.GetResponse(); //返回来自 Internet 资源的响应。 //respone.GetResponseStream() 获取用于写入请求数据的 Stream 对象 using (StreamReader stream = new StreamReader(respone.GetResponseStream(), Encoding.Default)) { //获取结果 resultStr = stream.ReadToEnd(); //读取数据 return resultStr; } } catch (Exception ex) { Response.Write(ex.Message); //输出错误信息 } return ""; } /// <summary> /// 获得判定qq是否在线的关键字:0表示不在线,1表示在线 /// </summary> /// <param name="qqnum">要判断的QQ号</param> /// <returns>1或是0</returns> public string GetQQStatue(string qqnum) { string str = GetURL(qqnum); int index = str.IndexOf('='); string strQQStute = str.Substring(index + 1, 1); return strQQStute; } } }
完整代码。直接可以运行的!
写写博客,方便自己也方便别人!