学习笔记httpwatch的使用

使用httpwatch可以在IE中查看每个动作的POST/GET详细参数.

配合HttpWebRequest抓取网站数据使用.


HttpWebRequest所需要的各种参数可以在httpwatch中很直观的看见.


数据抓取工具类:

/// <summary>
    /// 工具函数类
    /// </summary>
    public class MyHttp
    {
        public CookieContainer myCookieContainer = new CookieContainer();


        private string _EncodName = "gb2312";
        public string EncodName
        {
            set { _EncodName = value; }
        }


        public MyHttp()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }






        public void FreeCookie()
        {
            if (myCookieContainer != null)
            {
                myCookieContainer = null;
            }


            myCookieContainer = new CookieContainer();
        }


        public System.Drawing.Image GetWebImage(string url)
        {
            try
            {
                url.Trim();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.AllowAutoRedirect = true;
                req.Method = "GET";
                req.ProtocolVersion = HttpVersion.Version11;
                req.Headers.Set("x-requested-with", "XMLHttpRequest");
                req.Accept = "*/*";


                req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)";
                req.ContentType = "application/x-www-form-urlencoded";


                req.Timeout = 30000;
                req.CookieContainer = myCookieContainer;
                HttpWebResponse webresponse = null;
                try
                {
                    webresponse = (HttpWebResponse)req.GetResponse();
                    if (webresponse != null)
                    {
                        return System.Drawing.Image.FromStream(webresponse.GetResponseStream());
                    }
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (webresponse != null)
                    {
                        webresponse.Close();
                    }
                    if (req != null)
                    {
                        req.Abort();
                    }
                }


                return null;
            }
            catch
            {
                return null;
            }
        }
        private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
        {
            // trust any certificate!!!
            System.Console.WriteLine("Warning, trust any certificate");
            //为了通过证书验证,总是返回true
            return true;
        }
        public string dd = "";
        public string ngt = "";
        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.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
            //ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;//
            request.KeepAlive = true;
            request.AllowAutoRedirect = true;
            request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                            //application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
           // request.UserAgent = "CDR WapIE/2.07";


            request.Headers.Add("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Headers.Add("Accept-Language", "zh-cn");
            request.Timeout = 10000;




            if (!dd.Equals(""))
            {
                string[] dai = dd.Split(':');
                string ddd = dai[1].Trim();
                WebProxy proxyObject = new WebProxy(dai[0], Convert.ToInt32(ddd));//str为IP地址 port为端口号
                request.Proxy = proxyObject;
            }




            request.Headers.Add("Cookie", cok);


            request.CookieContainer = myCookieContainer;
            try
            {
                if (RequestUri != null && RequestUri != "")
                    request.Referer = RequestUri;
                else
                    request.Referer = request.RequestUri.ToString();
            }
            catch
            {
            }
            if (type == 2)
            {
                request.ContentType = "application/vnd.ms-excel; charset=utf-8";
            }
            else
            {
                request.ContentType = "application/x-www-form-urlencoded";
            }
            try
            {
                if (RequestUri != null && RequestUri != "")
                    request.Referer = RequestUri;
                else
                    request.Referer = request.RequestUri.ToString();
            }
            catch
            {
            }




            HttpWebResponse webresponse = null;
            try
            {
                if (postData.Length > 0)
                {
                    byte[] loginDataBytes = Encoding.ASCII.GetBytes(postData); //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)
                {
                    myCookieContainer.Add(webresponse.Cookies);
                    cok = request.CookieContainer.GetCookieHeader(request.RequestUri);
                    return GetResponseHTML(webresponse);
                }
            }
            catch
            {
            }
            finally
            {
                if (webresponse != null)
                {
                    webresponse.Close();
                }
            }


            return "";
        }
        public string cok = "";
      
        public int type = 0;
        public string downName;
        public string downUrl;
        public string GetResponseHTML(HttpWebResponse response)
        {


            if (type == 1)//读取图片
            {


                Stream fileStream = null;
                Stream responseStream = response.GetResponseStream();
                try
                {
                    File.Delete(System.Windows.Forms.Application.StartupPath + @"\mls");
                }
                catch
                {
                }
                fileStream = File.Create(System.Windows.Forms.Application.StartupPath + @"\mls" + ".jpg");
                byte[] bytes = new byte[1024];
                int count = 0;
                do
                {
                    count = responseStream.Read(bytes, 0, bytes.Length);
                    fileStream.Write(bytes, 0, count);
                }
                while (count > 0);


                responseStream.Close();
                fileStream.Close();




                return "33.png";
            }
            if (type == 2)//直接读取Excel
            {
                Stream fileStream = null;
                Stream responseStream = response.GetResponseStream();
                fileStream = File.Create(System.Windows.Forms.Application.StartupPath + @"\mls" + ".xls");
                byte[] bytes = new byte[1024];
                int count = 0;
                do
                {
                    count = responseStream.Read(bytes, 0, bytes.Length);
                    fileStream.Write(bytes, 0, count);
                }
                while (count > 0);


                responseStream.Close();
                fileStream.Close();




                object missing = System.Reflection.Missing.Value;
                string ret = string.Empty;
                Application excel = new Application();//lauch excel application
                if (excel == null)
                {
                }
                else
                {
                    excel.Visible = false; excel.UserControl = true;
                    // 以只读的形式打开EXCEL文件
                    Workbook wb = excel.Application.Workbooks.Open(System.Windows.Forms.Application.StartupPath + @"\mls" + ".xls", missing, true, missing, missing, missing,
                    missing, missing, missing, true, missing, missing, missing, missing, missing);
                    //取得第一个工作薄
                    Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);


                    //取得总记录行数 (包括标题列)
                    int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
                    //int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数


                    //取得数据范围区域 (不包括标题列)
                    Range tdr1 = ws.Cells.get_Range("A2", "A" + rowsint); //item
                    Range tdr2 = ws.Cells.get_Range("B2", "B" + rowsint); //item
                    //取得数据范围区域 (不包括标题列)
                    Range tdr3 = ws.Cells.get_Range("C2", "C" + rowsint); //item
                    Range tdr4 = ws.Cells.get_Range("D2", "D" + rowsint); //item
                    //取得数据范围区域 (不包括标题列)
                    Range tdr5 = ws.Cells.get_Range("E2", "E" + rowsint); //item
                    Range tdr6 = ws.Cells.get_Range("F2", "F" + rowsint); //item
                    Range tdr7 = ws.Cells.get_Range("G2", "G" + rowsint); //item






                    object[,] td1 = (object[,])tdr1.Value2; //get range’s value
                    object[,] td2 = (object[,])tdr2.Value2;
                    object[,] td3 = (object[,])tdr3.Value2;
                    object[,] td4 = (object[,])tdr4.Value2; //get range’s value
                    object[,] td5 = (object[,])tdr5.Value2;
                    object[,] td6 = (object[,])tdr6.Value2;
                    object[,] td7 = (object[,])tdr7.Value2;




                    for (int i = 0; i < rowsint - 1; i++)
                    {
                        if (!string.IsNullOrEmpty(ret))
                        {
                            ret += "^";
                        }
                        ret += td1[i + 1, 1].ToString() + "|" + td2[i + 1, 1].ToString() + "|" + td3[i + 1, 1].ToString() + "|" + td4[i + 1, 1].ToString() + "|" + td5[i + 1, 1].ToString() + "|" + td6[i + 1, 1].ToString() + "|" + td7[i + 1, 1].ToString();
                    }


                    excel.Quit();
                    excel = null;
                    Process[] procs = Process.GetProcessesByName("excel");


                    foreach (Process pro in procs)
                    {
                        pro.Kill();//没有更好的方法,只有杀掉进程
                    }
                    GC.Collect();
                }


                return ret;
            }


            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            /*Stream fileStream = null;
            string imgName = "b46b9e.torrent";




            Stream responseStream = response.GetResponseStream();
            fileStream = File.Create(imgName);
            byte[] bytes = new byte[1024];
            int count = 0;看
            do
            {
                count = responseStream.Read(bytes, 0, bytes.Length);
                fileStream.Write(bytes, 0, count);d
            }
            while (count > 0);
            */
            string strHTML = reader.ReadToEnd();


            if (reader != null)
            {
                reader.Close();
            }


            if (response != null)
            {
                response.Close();
            }


            return strHTML;
        }


        public string userid;
        public string pwd;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值