我想做一个程序,需要每隔5分钟去请求一个URL,在请求这个URL的时候会返回一些固定格式的信息,我要接收它,并存入数据库中,如果请求超时的话我需要程序提示报错
请问这个功能的实现思路是什么?具体需要用到哪些类呢?

解决方案 »

  1.   

    JS 设置settimeout 调用webservice或者一般处理程序。返回结果。我是这样考虑的
      

  2.   

    可以设置session过期时间,来判断。
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                
            }        public static string GetResponse(string url, string postData, string method, string RequestUri)
            {
                HttpWebRequest request = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create(url);
                }
                catch
                {
                    return "";
                }            if (request == null)
                    return "";            request.Method = method;
                request.Timeout = 300000;
                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.CookieContainer = new CookieContainer();            try
                {
                    if (RequestUri != null && RequestUri != "")
                        request.Referer = RequestUri;
                    else
                        request.Referer = request.RequestUri.ToString();
                }
                catch
                {
                }            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)
                    {
                        StreamReader reader = new StreamReader(webresponse.GetResponseStream(), Encoding.UTF8);
                        string strHTML = reader.ReadToEnd();                    if (reader != null)
                            reader.Close();                    return strHTML;
                    }
                }
                catch
                {
                }
                finally
                {
                    if (webresponse != null)
                    {
                        webresponse.Close();
                        webresponse = null;
                    }
                    if (request != null)
                    {
                        request.Abort();
                        request = null;
                    }
                }            return "";
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                string strHTML = GetResponse("http://www.sina.com");
                //正则表达式分析HTML代码
            }
        }
    }
      

  4.   

    HttpWebRequest 
    JS都成了.
      

  5.   

    System.Timers.Timer myTimer = new System.Timers.Timer();
    myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
    myTimer.Interval = 1000;
    myTimer.Enabled = true;
    private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("");
    myHttpWebRequest .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    myHttpWebRequest .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)";
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    Stream receiveStream = myHttpWebResponse.GetResponseStream();
    }