自动登陆post数据后,可以返回登陆后的url,但用Process.Start()打开这个url后提示没有的登陆,我想原因是CookieHeader并没有还原回去,有什么方法可以CookieHeader一起提交并打开页面呢?
代码:
                string cookieheader;
                request = WebRequest.Create(url) as HttpWebRequest;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                CookieContainer cookieCon = new CookieContainer();
                request.ContentLength = data.Length;
                request.CookieContainer = cookieCon;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;                cookieheader = request.CookieContainer.GetCookieHeader(new Uri(url));
                request.CookieContainer.SetCookies(new Uri(response.ResponseUri.ToString()),cookieheader);                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                System.Diagnostics.Process.Start(response.ResponseUri.ToString());

解决方案 »

  1.   

    给你一段代码看看吧
    /// <summary>
            /// 获取指定地址的html
            /// </summary>
            /// <param name="URL"></param>
            /// <param name="PostData"></param>
            /// <param name="encoding"></param>
            /// <returns></returns>
            public string GetHTML(string URL, string PostData, System.Text.Encoding encoding)
            {
                             
                try
                {
                    Variant.Err = "";                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
                    request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
                    if (this.Referer == "")
                    {
                        request.Referer = "http://community.csdn.net/";
                    }
                    else
                    {
                        request.Referer = this.Referer;
                    }
                    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
                    request.CookieContainer = this.ReqCookies;
                    
                    //设置代理
                    if(Setting.UseProxy)
                    {                    
                        if(!Setting.IEProxy)
                        {
                            WebProxy proxy = new WebProxy();
                            
                            try
                            {
                                proxy.Address = new Uri(String.Format("http://{0}:{1}", Setting.ProxyServer,  Setting.ProxyPort));
                                if (Setting.ProxyUsername.Length > 0 && Setting.ProxyPassword.Length > 0)
                                {
                                    proxy.Credentials = new NetworkCredential(Setting.ProxyUsername, Setting.ProxyPassword);
                                }
                                request.Proxy=proxy;
                            }
                            catch
                            {
                               
                            }
                        }                
                    }
                    else
                    {
                        request.Proxy = null;
                    }                //request.Timeout = 30000;
                    if (encoding == null)
                        encoding = Variant.Encoding;                //设置cookie
                    if (Variant.CurrentUser != null)
                    {
                        CookieCollection tmpCookies = Variant.CurrentUser.Cookies;                    System.Uri uri = new Uri(URL);                    foreach (System.Net.Cookie cookie in tmpCookies)
                        {
                            cookie.Domain = uri.Host;
                        }
                        request.CookieContainer.Add(tmpCookies);
                    }                //提交的数据
                    if (PostData != null && PostData.Length > 0)
                    {
                        request.ContentType = "application/x-www-form-urlencoded";
                        request.Method = "POST";                    byte[] b = encoding.GetBytes(PostData);
                        request.ContentLength = b.Length;
                        using (System.IO.Stream sw = request.GetRequestStream())
                        {
                            try
                            {                            sw.Write(b, 0, b.Length);
                            }
                            catch
                            {
                                _Html = "";
                                return _Html;
                            }
                            finally
                            {
                                if (sw != null) { sw.Close(); }
                            }
                        }                }                HttpWebResponse response = null;
                    System.IO.StreamReader sr = null;                try
                    {                    response = (HttpWebResponse)request.GetResponse();                    this.Cookies = response.Cookies;                    sr = new System.IO.StreamReader(response.GetResponseStream(), encoding);                    _Html = sr.ReadToEnd();                }
                    catch (WebException webex)
                    {
                        if (webex.Status == WebExceptionStatus.KeepAliveFailure)
                        {
                            _Html = "";                        return _Html;
                        }
                        else
                        {
                            _Html = "";                        return _Html;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        _Html = "";                    return _Html;
                    }
                    finally
                    {
                        if (sr != null) { sr.Close(); }
                        if (response != null) { response.Close(); }
                        response = null;
                        request = null;
                    }                return _Html;
                }
                catch
                {                
                    return "";
                }
            }