计算机编程

代码

巴黎人电玩, private void btnASPNET_Click(object sender, EventArgs e)
        {
巴黎人游戏官网,            Dictionary<string, string> postParams = new Dictionary<string, string>();
            postParams.Add("txtLoginId", "www.cnuunet.com");
            postParams.Add("txtPassword", "www.cnuunet.com");
            postParams.Add("btnLogin", "Sign in");
            textBox1.Text = GetAspNetCodeResponseDataFromWebSite(postParams, "", "");
        }

using System;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
 
namespace blogdata.nms_common
{
       ///
       ///定义获得数据流接口
       ///
       public interface IRequestable
       {
              Stream GetStream(string requestUrl);
       }
       ///
       /// Http 的摘要说明
       ///
       public class Http : IRequestable
       {
              ///
              ///设置请求的延时时间,以秒为单位
              ///
              private int mTimeOut = 1;
              ///
              /// ̉异常错误信息
              ///
              public string errorMessage;
              public string ErrorMessage
              {
                     get {return this.errorMessage;}
              }
              ///
              ///构造函数
              ///
              public Http(int timeOut)
              {
                     this.mTimeOut = timeOut * 1000;   
              }
              ///
              ///用方法隐藏成员
              ///
              ///
              ///
              public static Http GetIntance(int timeOut)
              {
                     return new Http(timeOut);
              }
              ///
              ///重载用方法隐藏成员,默认为1秒
              ///
              public static Http GetIntance()
              {
                     return new Http(1);
              }
 
              #region IRequestface ³ÉÔ±
              ///
              ///实现接口
              ///
              ///
              ///
              public Stream GetStream(string requestUrl)
              {
                     return GetStream(requestUrl,null);
              }
              ///
              ///重载接口
              ///
              ///
              ///
              ///
              public Stream GetStream(string requestUrl,byte[] postData)
              {
                     if (requestUrl == null || requestUrl.Length == 0) return null;
                     //判断有没有http://头
                     string httpHead = requestUrl.Substring(0,7);
                     if (httpHead.ToLower() != "http://") return null;
                     WebRequest wrq = WebRequest.Create(requestUrl);
                     wrq.Timeout = this.mTimeOut; //请求的时间,以秒为单位
                            if (postData != null)
                            {
                                   wrq.Method = "POST";
                                   wrq.ContentType =
                                          "application/x-www-form-urlencoded";
                                   wrq.ContentLength = postData.Length;
                                   Stream rs = wrq.GetRequestStream();
                                   rs.Write(postData,0,postData.Length);
                                   rs.Close();
                                   rs = null;
                            }
                     try
                     {
                            WebResponse wrp = wrq.GetResponse();
                            Stream res = wrp.GetResponseStream();
                            if(res != null) return res;
                     }
                     catch (WebException we)
                     {
                            //调试使用,也可以转化为流输出,我没有转化
                            this.errorMessage = we.Message;
                     }
                     return null;
              }
              #endregion
       }
}

        /// <summary>
        /// ASP.net页面登录方式 通过post密码方式读取网页内容
        /// 在本页面(login.aspx)的.cs文件内验证用户名和密码。
        /// Asp.net验证需要记住本次页面加载的__VIEWSTATE和__EVENTVALIDATION信息,并且需要button按钮的ID和Text信息
        /// </summary>
        /// <param name="postParams">用户名(用户名文本框的ID和内容)、密码(密码文本框的ID和内容)、summit按钮(button按钮的ID和Text)</param>
        /// <param name="getViewStateAndEventValidationLoginUrl">需要验证登陆信息的url</param>
        /// <param name="getDataUrl">需要抓取数据的网页url</param>
        /// <returns>抓取页面返回的html信息</returns>
        private string GetAspNetCodeResponseDataFromWebSite(Dictionary<string, string> postParams, string getViewStateAndEventValidationLoginUrl, string getDataUrl)
        {
            if (postParams == null || postParams.Keys.Count!=3)
            {
                string errorMessage = "参数中需要包含如下3个信息,缺一不可。用户名(用户名文本框的ID和内容)、密码(密码文本框的ID和内容)、summit按钮(button按钮的ID和Text)";
                MessageBox.Show(errorMessage);
                return errorMessage ;
            }

调用方法:
Stream gets = Http.GetIntance(timeout).GetStream(url);
//这里可以判断一下流
if (gets == null)
          Response.Write("error");
else
{
          //由于有些域名的访问是返回了北京宽带网或者其他的错误信息。因此不能捕获异常,结果为:流 !=null 通过读出流看一下数据,就明白了。
         Encoding encode =
         System.Text.Encoding.GetEncoding("gb2312");
         //读流
         StreamReader reader = new StreamReader(gets, encode);
         string str = reader.ReadToEnd().ToString();
         Response.Write(str);
}
注:
1、这里就不要捕获异常了。如果返回的为:null则就是发生了异常。因为我在类里面
已经处理异常了。
2、Url必须为完整的路径
即:带有

            try
            {
                CookieContainer cookieContainer = new CookieContainer();

<> <>

                ///////////////////////////////////////////////////
                // 1.打开 MyLogin.aspx 页面,获得 GetVeiwState & EventValidation
                ///////////////////////////////////////////////////                
                // 设置打开页面的参数
                HttpWebRequest request = WebRequest.Create(getViewStateAndEventValidationLoginUrl) as HttpWebRequest;
                request.Method = "GET";
                request.KeepAlive = false;

                // 接收返回的页面
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                System.IO.Stream responseStream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
                string srcString = reader.ReadToEnd();

                // 获取页面的 VeiwState,分析返回的页面,解析出__VIEWSTATE的值          
                string viewStateFlag = "id="__VIEWSTATE" value="";
                int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
                int j = srcString.IndexOf(""", i);
                string viewState = srcString.Substring(i, j - i);

                // 获取页面的 EventValidation,分析返回的页面,解析出__VIEWSTATE的值                   
                string eventValidationFlag = "id="__EVENTVALIDATION" value="";
                i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
                j = srcString.IndexOf(""", i);
                string eventValidation = srcString.Substring(i, j - i);

其他新闻
友情链接

公司名称巴黎人电玩
版权所有:Copyright © 2015-2019 http://www.zhongqiangjy.com. 巴黎人电玩有限公司 版权所有

友情链接

Copyright © 2015-2019 http://www.zhongqiangjy.com. 巴黎人电玩有限公司 版权所有
公司地址http://www.zhongqiangjy.com