通过后台事件直接调另一个页面的按钮事件,__EVENTVALIDATION,_VIEWSTATE

上几天做项目,需要将申请不合格的单子给提出来,作为内容重新发起一个新的流程。在此过程中,不需要打开页面,直接将数据保存到发起人草稿箱中。

那么在这过程中需要注意的是:

  1. 发起人信息这么样带过去
  2. 这么样调用该页面
  3. 数据如何保存

然后在网上找了一些demo,其中有一个是:

  1. 通过URL去请求页面get方式
  2. 将请求来的数据信息进行提取 ‘_**_VIEWSTATE**’,‘__EVENTVALIDATION’,视图状态,和按钮事件 (最后介绍一下__VIEWSTATE,__EVENTVALIDATION)
  3. 那么接下来需要post数据, Request request = new Request();将需要post的数据放在request.add(“key”,“value”)。例如: request.Add(“ctl00 contentPlace btnSave”, “保存草稿”);//为按钮的name名字
  4. 将第二步的数据当做数据源,添加到request .add中,然后post数据

具体代码:

     protected void Button1_Click(object sender, EventArgs e)
        {
              //   string url = "http://localhost:49857/test/WebForm6.aspx";
              string url = "http://localhost:1876/Workflow/MTStart.aspx?processID=359&ApplicantUserId=A150017";

            Request request = new Request();
            request.Method = Method.POST;
            #region  webForm6
            //request.Add("ctl00$ContentPlaceHolder1$TextBox1", "name111");
            //request.Add("ctl00$ContentPlaceHolder1$TextBox2", "name222");
            //request.Add("ctl00$ContentPlaceHolder1$TextBox3", "name3333");
            //request.Add("ctl00$ContentPlaceHolder1$Button1", "Butt11o22n");
            #endregion

            HttpRRHelper html = new HttpRRHelper();
            Response response = html.GetHTML(url,new WebProxy("172.27.1.250",80));
            Match math = Regex.Match(response.Html, "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(?<val>.*?)\" />", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            string value = math.Groups["val"].Value;

            request.Add("__VIEWSTATE", System.Web.HttpUtility.UrlEncode(value));
            math = Regex.Match(response.Html, "<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(?<val>.*?)\" />", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            request.Add("__EVENTVALIDATION", System.Web.HttpUtility.UrlEncode(math.Groups["val"].Value));
            request.Add("ctl00$contentPlace$btnSave", "保存草稿");//为按钮的name名字
          //  response = html.GetHTML(url, true, request, new WebProxy("172.27.1.250",80));
           // OAS.Common.HttpRRHelper.Init().BeginGetHTML(url, true, request);

            response = html.GetHTML(url, true, request, new WebProxy("172.27.1.250", 80));
        }

发送数据封装的方法,你可以把它作为新类使用:

namespace OAS.Common
{
    #region 发送方式
    public enum Method
    {
        GET, POST
    }
    #endregion

    #region 返回内容
    /// <summary>
    /// 返回内容
    /// </summary>
    public class Response
    {
        #region 远程服务器时间
        DateTime _RemoteDateTime = Convert.ToDateTime("1900-01-01 00:00:00.000");
        /// <summary>
        /// 远程服务器时间
        /// </summary>
        public DateTime RemoteDateTime
        {
            get { return _RemoteDateTime; }
            set { _RemoteDateTime = value; }
        }
        #endregion

        #region 返回内容
        string html = string.Empty;
        /// <summary>
        /// 返回内容
        /// </summary>
        public string Html
        {
            get { return html; }
            set { html = value; }
        }
        #endregion

        #region 返回的Cookies
        CookieContainer _Cookies = new CookieContainer();
        /// <summary>
        /// 返回的Cookies
        /// </summary>
        public CookieContainer Cookies
        {
            get { return _Cookies; }
            set { _Cookies = value; }
        }
        #endregion

        #region HTTP状态代码
        /// <summary>
        /// HTTP状态代码
        /// </summary>
        public HttpStatusCode StatusCode { get; set; }
        #endregion

        #region 返回的图片内容
        Image _Image = null;
        /// <summary>
        /// 返回的图片内容
        /// </summary>
        public Image Image
        {
            get { return _Image; }
            set { _Image = value; }
        }
        #endregion

        #region 当前URL
        string _Url = string.Empty;
        /// <summary>
        /// 当前URL
        /// </summary>
        public string Url
        {
            get { return _Url; }
            set { _Url = value; }
        }
        #endregion
    }
    #endregion

    #region 发送数据
    /// <summary>
    /// 发送数据
    /// </summary>
    [Serializable]
    public class Request
    {
        List<KeyValuePair<string, string>> sList = new List<KeyValuePair<string, string>>();
        Method _sendMethod = Method.GET;
        public Method Method
        {
            get { return _sendMethod; }
            set { _sendMethod = value; }
        }

        private CookieContainer _Cookies = new CookieContainer();
        public CookieContainer Cookies
        {
            get { return this._Cookies; }
            set { this._Cookies = value; }
        }
        public void Clear()
        {
            this.sList.Clear();
        }
        public void Add(string key, string value)
        {
            this.sList.Add(new KeyValuePair<string, string>(key, value));
        }
        public void Update(string key, string value)
        {
            int index = -1;
            for (int i = 0; i < this.List.Count; i++)
            {
                if (this.List[i].Key.Equals(key))
                {
                    index = i;
                    break;
                }
            }

            if (index > 0) this.List.RemoveAt(index);

            this.List.Add(new KeyValuePair<string, string>(key, value));
        }
        public override string ToString()
        {
            string sRet = string.Empty;

            foreach (KeyValuePair<string, string> val in this.sList)
            {
                if (sRet.Length == 0)
                    sRet = string.Format("{0}={1}", val.Key, val.Value);
                else
                    sRet = string.Format("{0}&{1}={2}", sRet, val.Key, val.Value);
            }

            return sRet;
        }

        public byte[] ToBytes()
        {
            return Encoding.ASCII.GetBytes(this.ToString());
        }

        public List<KeyValuePair<string, string>> List
        {
            set
            {
                this.sList = value;
            }
            get
            {
                return this.sList;
            }
        }

        public string Find(string key)
        {
            string sRet = string.Empty;

            KeyValuePair<string, string> val = this.List.Find(delegate(KeyValuePair<string, string> k) { return k.Key.Equals(key); });

            sRet = val.Value;

            return sRet;
        }

    }
    #endregion

    public class HttpRRHelper
    {
        #region Static Field

        private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

        #endregion Static Field

        public static HttpRRHelper Init()
        {
            return new HttpRRHelper();
        }

        #region Cookies
        CookieContainer _Cookies = new CookieContainer();
        /// <summary>
        /// Cookies
        /// </summary>
        public CookieContainer Cookies
        {
            get { return _Cookies; }
            set { _Cookies = value; }
        }
        #endregion

        #region 连接远程服务器超时触发事件
        public delegate void Connection_TimeOut_Handle(string sUrl, Request request, Exception ex);
        /// <summary>
        /// 连接远程服务器超时触发事件
        /// </summary>
        public event Connection_TimeOut_Handle Connection_TimeOut;
        #endregion

        #region 获取数据完毕触发事件
        public delegate void Connection_Complete_Handle(Response response);
        /// <summary>
        ///  获取数据完毕触发事件
        /// </summary>
        public event Connection_Complete_Handle Connection_Complete;
        #endregion

        #region 设置发送头信息
        /// <summary>
        /// 设置发送头信息
        /// </summary>
        /// <param name="request"></param>
        private void SetRequestHeader(ref HttpWebRequest request, string referer)
        {
            request.Timeout = 5000;
            request.Accept = "*/*";
            request.UserAgent = DefaultUserAgent;//"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.50727;)";
            request.Headers["Accept-Encoding"] = "gzip, deflate";
            request.Headers["Accept-Language"] = "zh-cn";
            request.Headers["Accept-Charset"] = "utf-8;q=0.7,*;q=0.7";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Referer = referer;
            request.KeepAlive = true;
        }
        #endregion

        #region 解压经过Gzip压缩的流
        /// <summary>
        /// 解压经过Gzip压缩的流
        /// </summary>
        /// <param name="contentEncoding">流编码</param>
        /// <param name="resonseStream">网页返回的流内容</param>
        /// <returns></returns>
        private string GzipStreame(string ecoding, Stream resonseStream)
        {
            return GzipStreame(ecoding, resonseStream, Encoding.UTF8);
        }
        private string GzipStreame(string ecoding, Stream resonseStream, Encoding coding)
        {
            string html = string.Empty;
            if (ecoding.ToLower().IndexOf("gzip") != -1)
            {
                html = new StreamReader(new GZipStream(resonseStream, CompressionMode.Decompress), coding).ReadToEnd();
            }
            else if (ecoding.ToLower().IndexOf("deflate") >= 0)
            {
                html = new StreamReader(new DeflateStream(resonseStream, CompressionMode.Decompress), coding).ReadToEnd();
            }
            else
            {
                html = new StreamReader(resonseStream, coding).ReadToEnd();
            }
            return html;
        }

        #endregion

        #region 获取远程HTML
        /// <summary>
        /// 获取远程HTML
        /// </summary>
        /// <param name="url">地址</param>
        /// <returns></returns>
        public Response GetHTML(string url)
        {
            return GetHTML(url, false, new Request(), new Uri(url).Host, Encoding.UTF8, null);
        }
        public Response GetHTML(string url, WebProxy proxy)
        {
            return GetHTML(url, false, new Request(), new Uri(url).Host, Encoding.UTF8, proxy);
        }
        public Response GetHTML(string url, Encoding coding)
        {
            return GetHTML(url, false, new Request(), new Uri(url).Host, coding, null);
        }
        public Response GetHTML(string url, Encoding coding, WebProxy proxy)
        {
            return GetHTML(url, false, new Request(), new Uri(url).Host, coding, proxy);
        }
        /// <summary>
        /// 获取远程HTML
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="send">是否自动重定向URL</param>
        /// <returns></returns>
        public Response GetHTML(string url, bool bAllowAutoRedirect)
        {
            return GetHTML(url, bAllowAutoRedirect, new Request() { }, new Uri(url).Host, Encoding.UTF8, null);
        }
        public Response GetHTML(string url, bool bAllowAutoRedirect, WebProxy porxy)
        {
            return GetHTML(url, bAllowAutoRedirect, new Request() { }, new Uri(url).Host, Encoding.UTF8, porxy);
        }
        /// <summary>
        /// 获取远程HTML
        /// </summary>
        /// <param name="sUrl">地址</param>
        /// <param name="send">发送内容</param>
        /// <returns></returns>
        public Response GetHTML(string url, Request request)
        {
            return GetHTML(url, request, Encoding.UTF8);
        }
        public Response GetHTML(string url, Request request, WebProxy porxy)
        {
            return GetHTML(url, false, request, new Uri(url).Host, Encoding.UTF8, porxy);
        }
        public Response GetHTML(string url, bool bAllowAutoRedirect, Request request)
        {
            return GetHTML(url, bAllowAutoRedirect, request, new Uri(url).Host, Encoding.UTF8, null);
        }
        public Response GetHTML(string url, bool bAllowAutoRedirect, Request request, WebProxy proxy)
        {
            return GetHTML(url, bAllowAutoRedirect, request, new Uri(url).Host, Encoding.UTF8, proxy);
        }
        public Response GetHTML(string url, Request request, Encoding coding)
        {
            return GetHTML(url, false, request, new Uri(url).Host, coding, null);
        }
        public Response GetHTML(string url, Request request, Encoding coding, WebProxy proxy)
        {
            return GetHTML(url, false, request, new Uri(url).Host, coding, proxy);
        }
        /// <summary>
        /// 获取远程HTML
        /// </summary>
        /// <param name="sUrl">地址</param>
        /// <param name="bAllowAutoRedirect">是否自动跳转</param>
        /// <param name="send">发送内容</param>
        /// <returns></returns>
        public Response GetHTML(string sUrl, bool bAllowAutoRedirect, Request request, string referer)
        {
            return GetHTML(sUrl, bAllowAutoRedirect, request, referer, Encoding.UTF8, null);
        }
        public Response GetHTML(string sUrl, bool bAllowAutoRedirect, Request request, string referer, WebProxy proxy)
        {
            return GetHTML(sUrl, bAllowAutoRedirect, request, referer, Encoding.UTF8, proxy);
        }
        /// <summary>
        /// 获取HTML页面内容
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="bAllowAutoRedirect">是否自动跳转</param>
        /// <param name="request">查询信息</param>
        /// <param name="referer">引用页</param>
        /// <param name="coding">编码格式</param>
        /// <returns></returns>
        public Response GetHTML(string url, bool bAllowAutoRedirect, Request request, string referer, Encoding coding, WebProxy proxy)
        {
            Response result = new Response();

            //if (request.Method == Method.GET)
            //{
            //    string sData = request.ToString();
            //    if (sData.Length > 0) url = string.Format("{0}?{1}", url, sData);
            //}

            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            if (proxy != null) httpRequest.Proxy = proxy;

            #region SSL方式
            if (url.Contains("https://"))
            {
                //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                //创建证书文件
                X509Certificate objx509 = new X509Certificate();

                //添加到请求里
                httpRequest.ClientCertificates.Add(objx509);
            }
            #endregion

            // 设置发送头信息
            if (referer.Length == 0)
                SetRequestHeader(ref httpRequest, new Uri(url).Host);
            else
                SetRequestHeader(ref httpRequest, referer);
            // 自动重定向
            httpRequest.AllowAutoRedirect = bAllowAutoRedirect;
            httpRequest.Timeout = 60 * 1000;
            // 关联Cookies
            //if (httpRequest.CookieContainer == null)
            //    httpRequest.CookieContainer = new CookieContainer();
            httpRequest.CookieContainer = this.Cookies;

            try
            {

                if (request.Method == Method.POST)
                {
                    // 设置发送方式
                    httpRequest.Method = request.Method.ToString();
                    // 获取发送数据流
                    //HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;

                    Stream strem = httpRequest.GetRequestStream();

                    // 写入发送数据
                    byte[] bs = request.ToBytes();
                    strem.Write(bs, 0, bs.Length);
                    strem.Close();
                }
                #region 添加授权
                //2017/05-10
                NetworkCredential d = new NetworkCredential("A150018", "936");//添加此代码
                httpRequest.Credentials = d;
                #endregion



                HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;

                Stream responseStream = httpResponse.GetResponseStream();

                StreamReader sr = new StreamReader(responseStream, coding);

                //result.Html = sr.ReadToEnd();
                result.Html = System.Web.HttpUtility.HtmlDecode(GzipStreame(httpResponse.ContentEncoding, responseStream, coding));
                result.RemoteDateTime = Convert.ToDateTime(httpResponse.GetResponseHeader("Date"));

                result.Url = httpResponse.ResponseUri.OriginalString;

                this.Cookies.Add(httpResponse.Cookies);

                result.Cookies = this.Cookies;

                result.StatusCode = httpResponse.StatusCode;

                responseStream.Dispose();

                if (this.Connection_Complete != null)
                    this.Connection_Complete.Invoke(result);
            }
            catch (Exception ex)
            {
                if (this.Connection_TimeOut != null)
                    this.Connection_TimeOut.Invoke(url, request, ex);
            }
            return result;
        }

        private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

        #endregion

        #region 获取远程图片
        public Response GetImage(string sUrl)
        {
            return GetImage(sUrl, false, new Request());
        }
        public Response GetImage(string sUrl, WebProxy proxy)
        {
            return GetImage(sUrl, false, new Request(), proxy);
        }
        public Response GetImage(string sUrl, string referer)
        {
            return GetImage(sUrl, false, referer, null);
        }
        public Response GetImage(string sUrl, string referer, WebProxy proxy)
        {
            return GetImage(sUrl, false, referer, proxy);
        }
        public Response GetImage(string sUrl, bool bAllowAutoRedirect, string referer)
        {
            return GetImage(sUrl, bAllowAutoRedirect, new Request(), referer, null);
        }
        public Response GetImage(string sUrl, bool bAllowAutoRedirect, string referer, WebProxy proxy)
        {
            return GetImage(sUrl, bAllowAutoRedirect, new Request(), referer, null);
        }
        //public Image GetImage(string sUrl, bool bAllowAutoRedirect, Request request)
        //{
        //    return GetImage(sUrl, bAllowAutoRedirect, request, new CookieContainer(), "");
        ////}
        //public Image GetImage(string sUrl, bool bAllowAutoRedirect, Request request, string referer)
        //{
        //    return GetImage(sUrl, bAllowAutoRedirect, request,referer);
        //}
        /// <summary>
        /// 获取远程图片
        /// </summary>
        /// <param name="sUrl">图片地址</param>
        /// <param name="bAllowAutoRedirect">是否自动跳转</param>
        /// <param name="send">发送内容</param>
        /// <returns></returns>
        public Response GetImage(string sUrl, bool bAllowAutoRedirect, Request request)
        {
            return GetImage(sUrl, bAllowAutoRedirect, request, "", null);
        }
        public Response GetImage(string sUrl, bool bAllowAutoRedirect, Request request, string referer)
        {
            return GetImage(sUrl, bAllowAutoRedirect, request, referer, null);
        }
        public Response GetImage(string sUrl, bool bAllowAutoRedirect, Request request, WebProxy proxy)
        {
            return GetImage(sUrl, bAllowAutoRedirect, request, "", proxy);
        }
        /// <summary>
        /// 获取远程图片
        /// </summary>
        /// <param name="sUrl">图片地址</param>
        /// <param name="bAllowAutoRedirect">是否自动跳转</param>
        /// <param name="send">发送内容</param>
        /// <returns></returns>
        public Response GetImage(string url, bool bAllowAutoRedirect, Request request, string referer, WebProxy proxy)
        {
            Response response = new Response();

            if (request.Method == Method.GET)
            {
                string sData = request.ToString();
                if (sData.Length > 0) url = string.Format("{0}?{1}", url, sData);
            }

            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            if (proxy != null) httpRequest.Proxy = proxy;

            #region SSL方式
            if (url.Contains("https://"))
            {
                //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                //创建证书文件
                X509Certificate objx509 = new X509Certificate();

                //添加到请求里
                httpRequest.ClientCertificates.Add(objx509);
            }
            #endregion

            // 设置发送头信息
            if (referer.Length == 0)
                SetRequestHeader(ref httpRequest, new Uri(url).Host);
            else
                SetRequestHeader(ref httpRequest, referer);

            // 自动重定向
            httpRequest.AllowAutoRedirect = bAllowAutoRedirect;
            // 关联Cookies
            httpRequest.CookieContainer = this.Cookies;

            try
            {

                if (request.Method == Method.POST)
                {
                    // 设置发送方式
                    httpRequest.Method = request.Method.ToString();
                    // 获取发送数据流
                    //HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;

                    Stream strem = httpRequest.GetRequestStream();

                    // 写入发送数据
                    byte[] bs = request.ToBytes();
                    strem.Write(bs, 0, bs.Length);
                    strem.Close();
                }

                HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;

                Stream responseStream = httpResponse.GetResponseStream();

                //if (responseStream.CanSeek)
                //{
                //response.Image = Image.FromStream(responseStream);
                //}
                //else
                //{
                //   response.Image = null;
                //}
                response.RemoteDateTime = Convert.ToDateTime(httpResponse.GetResponseHeader("Date"));

                this.Cookies.Add(httpResponse.Cookies);
                response.Cookies = this.Cookies;
                response.StatusCode = httpResponse.StatusCode;
                responseStream.Dispose();
                responseStream.Close();
            }
            catch (Exception ex)
            {
                if (this.Connection_TimeOut != null)
                    this.Connection_TimeOut.Invoke(url, request, ex);
            }

            return response;
        }
        #endregion

        #region 异步获取远程HTML
        /// <summary>
        /// 异步获取远程HTML
        /// </summary>
        /// <param name="sUrl">地址</param>
        /// <returns></returns>
        public void BeginGetHTML(string sUrl)
        {
            BeginGetHTML(sUrl, false, new Request() { });
        }
        /// <summary>
        /// 异步获取远程HTML
        /// </summary>
        /// <param name="sUrl">地址</param>
        /// <param name="send">是否自动重定向URL</param>
        /// <returns></returns>
        public void BeginGetHTML(string sUrl, bool bAllowAutoRedirect)
        {
            BeginGetHTML(sUrl, bAllowAutoRedirect, new Request() { });
        }
        /// <summary>
        /// 异步获取远程HTML
        /// </summary>
        /// <param name="sUrl">地址</param>
        /// <param name="send">发送内容</param>
        /// <returns></returns>
        public void BeginGetHTML(string sUrl, Request request)
        {
            BeginGetHTML(sUrl, false, request);
        }
        /// <summary>
        /// 异步获取远程HTML
        /// </summary>
        /// <param name="sUrl">地址</param>
        /// <param name="bAllowAutoRedirect">是否自动跳转</param>
        /// <param name="send">发送内容</param>
        /// <returns></returns>
        public void BeginGetHTML(string sUrl, bool bAllowAutoRedirect, Request request)
        {
            Response result = new Response();

            if (request.Method == Method.GET)
            {
                string sData = request.ToString();
                if (sData.Length > 0) sUrl = string.Format("{0}?{1}", sUrl, sData);
            }

            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(sUrl);

            #region 添加授权
            //2017/05-10
            NetworkCredential d = new NetworkCredential("A150028", "936");//添加此代码
            httpRequest.Credentials = d;
            #endregion

            // 设置发送头信息
            SetRequestHeader(ref httpRequest, new Uri(sUrl).Host);
            // 自动重定向
            httpRequest.AllowAutoRedirect = bAllowAutoRedirect;

            if (request.Method == Method.POST)
            {
                // 设置发送方式
                httpRequest.Method = request.Method.ToString();

                // 获取发送数据流
                Stream stream = httpRequest.GetRequestStream();

                byte[] bs = request.ToBytes();
                stream.Write(bs, 0, bs.Length);
                stream.Close();
                //httpRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),httpRequest);
                // 写入发送数据

            }

            //HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;

            AsyncCallback callback = new AsyncCallback(GetResponseStreamCallback);
            try
            {
                httpRequest.BeginGetResponse(callback, httpRequest);
            }
            catch (WebException ex)
            {
                if (this.Connection_TimeOut != null)
                    this.Connection_TimeOut.Invoke(sUrl, request, ex);
            }
        }

        private void GetRequestStreamCallback(IAsyncResult ar)
        {
            //Request request = ar.AsyncState as Request;
            //Stream stream = async.EndGetRequestStream(ar);

            //byte[] bs = request.ToBytes();
            //stream.Write(bs, 0, bs.Length);
            //stream.Close();
        }

        private void GetResponseStreamCallback(IAsyncResult ar)
        {
            HttpWebRequest httpRequest = ar.AsyncState as HttpWebRequest;
            #region 添加授权
            //2017/05-10
            NetworkCredential d = new NetworkCredential("A150027", "936");//添加此代码
            httpRequest.Credentials = d;
            #endregion

            HttpWebResponse httpResponse = httpRequest.EndGetResponse(ar) as HttpWebResponse;





            Stream responseStream = httpResponse.GetResponseStream();

            Response response = new Response();

            response.Html = GzipStreame(httpResponse.ContentEncoding, responseStream);
            response.RemoteDateTime = Convert.ToDateTime(httpResponse.GetResponseHeader("Date"));
            //response.Cookies = httpResponse.Cookies;

            if (this.Connection_Complete != null)
                this.Connection_Complete.Invoke(response);
        }
        #endregion
    }
}

完美解决

__VIEWSTATE

ViewState是ASP.NET中用来保存WEB控件回传时状态值一种机制。在WEB窗体(FORM)的设置为runat=”server”,这个窗体(FORM)会被附加一个隐藏的属性_VIEWSTATE。_VIEWSTATE中存放了所有控件在ViewState中的状态值。

ViewState是类Control中的一个域,其他所有控件通过继承Control来获得了ViewState功能。它的类型是system.Web.UI.StateBag,一个名称/值的对象集合。

当请求某个页面时,ASP.NET把所有控件的状态序列化成一个字符串,然后做为窗体的隐藏属性送到客户端。当客户端把页面回传时,ASP.NET分析回传的窗体属性,并赋给控件对应的值

_EVENTVALIDATION

__EVENTVALIDATION只是用来验证事件是否从合法的页面发送,只是一个数字签名,所以一般很短。

“id”属性为“__EVENTVALIDATION”的隐藏字段是ASP.NET 2.0的新增的安全措施。该功能可以阻止由潜在的恶意用户从浏览器端发送的未经授权的请求.

为了确保每个回发和回调事件来自于所期望的用户界面元素,ASP.NET运行库将在事件中添加额外的验证层。服务器端通过检验表单提交请求的内容,将其与“id”属性为“__EVENTVALIDATION”隐藏字段中的信息进行匹配。根据匹配结果来验证未在浏览器端添加额外的输入字段(有可能为用户在浏览器端恶意添加的字段),并且该值是在服务器已知的列表中选择的。ASP.NET运行库将在生成期间创建事件验证字段,而这是最不可能获取该信息的时刻。像视图状态一样,事件验证字段包含散列值以防止发生浏览器端篡改。

说明:“id”属性为“__EVENTVALIDATION”隐藏字段一般在表单的最下方,如果表单在浏览器端尚未解析完毕时,用户提交数据有可能导致验证失败。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>