想实现一个客户端,网站新闻更新,主动通知客户端更新,弹出消息提示框,类似QQ的新闻订阅?
想实现一个客户端,网站新闻更新,主动通知客户端更新,弹出消息提示框,类似QQ的新闻订阅。请问:有什么方案? --------------------编程问答-------------------- 用Socket吧,服务器得知新闻更新后,主动通知客户端!
前提是服务器得知道当前所有在线的客户端! --------------------编程问答--------------------
服务器怎么得知新闻有更新? --------------------编程问答-------------------- 定时的请求,web可以用setTimeout(function(),time);c/s可用定时timer --------------------编程问答-------------------- 抓取网页。。。。。
几秒抓一次新闻网页。。获取所有新闻放到集合里面处理数据
判断是否有新纪录
有就,自己弄了哦 。
--------------------编程问答-------------------- 楼主给分!!!!!!!!!!
保存所有抓取到的新闻表题就行了。 都添加到Dictionary 里面去。 再用 Dictionary判断是否有新的。 哈哈 。
忘了。 最好用多线程处理啊
(*^__^*) 嘻嘻……!!!!!!
送个抓取代码给你。
--------------------编程问答--------------------
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Collections.Generic;
using System.Net;
using System.Drawing;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace Yiwai
{
/// <summary>
/// 工具函数类
/// </summary>
public class clsHttp
{
private CookieContainer myCookieContainer = new CookieContainer();
private string _EncodName = "gb2312";
public string EncodName
{
set { _EncodName = value; }
}
public clsHttp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string MD5(string text)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(text, "MD5").ToLower();
}
public void FreeCookie()
{
if (myCookieContainer != null)
{
myCookieContainer = null;
}
myCookieContainer = new CookieContainer();
}
public Image GetWebImage(string url)
{
try
{
url.Trim();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.AllowAutoRedirect = true;
req.Method = "GET";
// req.Headers.Add("Accept-Language", "zh-cn");
req.Headers.Add("Accept-Encoding", "gzip,deflate");
// req.Headers.Add("P3P", "CP=CAO PSA OUR");
//req.Headers.Add("Cookie", "LangKey=cs;");
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
req.CookieContainer = myCookieContainer;// new CookieContainer();
req.KeepAlive = true;
req.Timeout = 30000;
HttpWebResponse webresponse = null;
try
{
webresponse = (HttpWebResponse)req.GetResponse();
if (webresponse != null)
{
return Image.FromStream(webresponse.GetResponseStream());
}
}
catch
{
return null;
}
finally
{
if (webresponse != null)
{
webresponse.Close();
}
if (req != null)
{
req.Abort();
}
}
return null;
}
catch
{
return null;
}
}
public string GetResponse(string url, string postData, string method, string RequestUri)
{
url.Trim();
HttpWebRequest request = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
}
catch
{
return "";
}
if (request == null)
return "";
request.Method = method;
request.Timeout = 333338;
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";
// request.Headers.Add("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
//request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.5");
//request.Headers.Add("Cookie", "LangKey=cs;");
request.CookieContainer = myCookieContainer;
try
{
if (RequestUri != null && RequestUri != "")
request.Referer = RequestUri;
else
request.Referer = request.RequestUri.ToString();
}
catch
{
Console.WriteLine("出错3");
}
request.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse webresponse = null;
try
{
if (postData.Length > 0)
{
byte[] loginDataBytes = Encoding.Default.GetBytes(postData);
request.ContentLength = loginDataBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
}
webresponse = request.GetResponse() as HttpWebResponse;
if (webresponse != null)
{
return GetResponseHTML(webresponse);
}
}
catch
{
Console.WriteLine("出错");
}
finally
{
if (webresponse != null)
{
webresponse.Close();
}
if (request != null)
{
request.Abort();
}
}
return "";
}
private string GetResponseHTML(HttpWebResponse response)
{
string strHTML;
try
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
strHTML = reader.ReadToEnd();
strHTML = strHTML.Replace("\n", "");
strHTML = strHTML.Replace("\r", "");
strHTML = strHTML.Replace("\t", "");
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
}
catch (Exception)
{
Console.WriteLine("出错11");
return "";
}
return strHTML;
}
}
}
谢谢了 --------------------编程问答-------------------- 可以用委托
新闻类增加新闻时,触发事件 向客户端发送信息,客户端接收后弹出对话框 --------------------编程问答--------------------
能否再详细点儿?网站是用PHP写的。 --------------------编程问答-------------------- 用服务器推技术
补充:.NET技术 , C#