using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;namespace register
{
    public partial class FormRegister : Form
    {
        private const string NET_SESSIONID = "ASP.NET_SessionId=";
        private const string CLIENTKEY = "ClientKey=";        string aspcookie = "";
        private string html = "";
        private string sessionId = "";
        private string clientKey = "";
        private string viewState = "";
        private string eventvalidaction = "";        public FormRegister()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            HttpWebRequest request = WebRequest.Create("http://register.sdo.com/PTNew/input_pt.aspx") as HttpWebRequest;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Accept = "*/*";
            request.Referer = "http://register.sdo.com/PTNew/input_pt.aspx";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
            request.Method = "GET";
            request.KeepAlive = true;
            request.Headers.Set("Accept-Language", "zh-CN");            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();            string coder = ((HttpWebResponse)response).CharacterSet;
            //this.textBox4.Text = coder;
            System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding(coder));
            html = reader.ReadToEnd();            string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
            int i = html.IndexOf(viewStateFlag) + viewStateFlag.Length;
            int j = html.IndexOf("\"", i);
            viewState = html.Substring(i, j - i);            string eventvalidactionFlag = "id=\"__EVENTVALIDATION\" value=\"";
            int x = html.IndexOf(eventvalidactionFlag) + eventvalidactionFlag.Length;
            int y = html.IndexOf("\"", x);
            eventvalidaction = html.Substring(x, y - x);            aspcookie = response.Headers.Get("Set-Cookie");
            sessionId = GetSessionId();
            clientKey = GetClientKey();
            //this.textBox3.Text = html;            
        }        /// <summary>
        /// 获取ClientKey
        /// </summary>
        /// <returns></returns>
        private string GetClientKey()
        {
            // 这个仅仅是用来获取ClientKey值的长度
            string id = "bb32434c-3bb3-4e44-92c3-8952f631ca87";
            int index = aspcookie.IndexOf(CLIENTKEY) + CLIENTKEY.Length;
            String str = aspcookie.Substring(index, id.Length);
            return str;
        }        /// <summary>
        /// 获取ASP.NET_SessionId=
        /// </summary>
        /// <returns></returns>
        private string GetSessionId()
        {
            string id = "waw51w55ezclzf451rax4245";
            int index = aspcookie.IndexOf(NET_SESSIONID) + NET_SESSIONID.Length;
            String str = aspcookie.Substring(index, id.Length);
            return str;
        }        private void button1_Click(object sender, EventArgs e)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;            //string randnum = this.textBox3.Text;
            string password = textBox2.Text;
            string loginname = textBox1.Text;            //加__EVENTVALIDATION  就会跑到注册页面
            //删__EVENTVALIDATION  对不起,目前系统繁忙,请稍后尝试???????????????????/
            string postData = "username={0}&password={1}&__VIEWSTATE={2}&sessionid={3}&__EVENTVALIDATION={4}&chkAgreement=1&chkPromoRegSpreadr=1&ptType=0&chksdid=false&agent4IpCheck=checkIp&txtRegisterFrom=0&txtRegisterZone=0";
            postData = string.Format(postData, System.Web.HttpUtility.UrlEncode(loginname), System.Web.HttpUtility.UrlEncode(password), System.Web.HttpUtility.UrlEncode(viewState), System.Web.HttpUtility.UrlEncode(sessionId), System.Web.HttpUtility.UrlEncode(eventvalidaction));
            byte[] byteRequest = Encoding.GetEncoding("gb2312").GetBytes(postData);            CookieCollection cookies = new CookieCollection();
            cookies.Add(new Cookie("ASP.NET_SessionId", sessionId));
            cookies.Add(new Cookie("REG_VALIDATE_CODE", "hide"));
            cookies.Add(new Cookie("SDO_REG_FROM", "0"));   
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://register.sdo.com/PTNew/input_pt.aspx");            CookieContainer co = new CookieContainer();
            co.Add(new Uri("http://register.sdo.com/PTNew/input_pt.aspx"), cookies);
            httpWebRequest.CookieContainer = co;
 
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, */*";
            httpWebRequest.Referer = "http://register.sdo.com/PTNew/input_pt.aspx";
            httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
            httpWebRequest.Method = "POST";
            httpWebRequest.KeepAlive = true;
            httpWebRequest.Headers.Set("Cache-Control", "no-cache");
            //httpWebRequest.Headers.Set("Accept-Encoding", "gzip, deflate");
            httpWebRequest.Headers.Set("Accept-Language", "zh-CN");
           
            httpWebRequest.ContentLength = byteRequest.Length;
            Stream stream;
            stream = httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();            try
            {
                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            }
            catch (WebException ex)
            {
                webResponse = (HttpWebResponse)ex.Response;
            }
            System.IO.Stream responseStream = webResponse.GetResponseStream();
            String header = webResponse.Headers.ToString();
            Stream getStream = webResponse.GetResponseStream();
            long contentLength = webResponse.ContentLength;            byte[] outBytes = new byte[contentLength];
            outBytes = ReadFully(getStream);
            getStream.Close();            getStream = new MemoryStream(outBytes);
            StreamReader streamReader = new StreamReader(getStream, Encoding.GetEncoding("gb2312"));
            string getString = streamReader.ReadToEnd();
            streamReader.Close();
            getStream.Close();            this.textBox3.Text = getString;
        }        static byte[] ReadFully(Stream stream)
        {
            byte[] buffer = new byte[128];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }
    }    
}现在问题:streamReader.ReadToEnd()返回为http://register.sdo.com/PTNew/input_pt.aspx 源码,没有进到里面去为什么?

解决方案 »

  1.   

    是指调试没进到里面去吗?
    因为你没有.net framework的源代码和pdb.
      

  2.   


    谢谢你
    不是,比如我做了一个csdn的模拟登录可以提示登录成功但这个streamReader.ReadToEnd() 返回的还是注册页面源码是不是少了什么参数呀,但我看注册页面源码没什么参数了。
      

  3.   

    to :noway8881可不可以帮我看看呀,谢谢啦
      

  4.   

    getStream监视一下这个变量,估计是这个有问题
      

  5.   

    盛大注册共分三次post 提交,每次请求你都需要请求可以用工具观察下,希望对你有帮助
      

  6.   

    你抓包看下数据啊~~request.Referer = "http://register.sdo.com/PTNew/input_pt.aspx";