cookie 本来就是会提交上去的

解决方案 »

  1.   

     private CookieContainer cc = new CookieContainer();//实例
        static System.Text.Encoding encoding = System.Text.Encoding.UTF8;
        public string Post(string url, NameValueCollection nvc)//提交数据
        {
            StringBuilder sb = new StringBuilder();
            foreach (string key in nvc.AllKeys)
            {
                sb.Append(key + "=" + HttpUtility.UrlEncode(nvc[key]) + "&");
            }
            HttpWebRequest wc = (HttpWebRequest)HttpWebRequest.Create(url);
            wc.CookieContainer = cc;
            if (!string.IsNullOrEmpty(sb.ToString()))
            {
                wc.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*";
                wc.ContentType = "application/x-www-form-urlencoded";
                wc.Referer = "http://oa.plusbe.com/Admin/Login.aspx";
                wc.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.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; Shuame)";
                wc.Headers.Add("Accept-Language", "zh-cn");
                wc.Headers.Add("UA-CPU", "x86");
                wc.AllowAutoRedirect = true;
                wc.Method = "POST";
                string postData = sb.ToString().TrimEnd('&');
                byte[] postBuffer = encoding.GetBytes(postData);
                wc.ContentLength = postBuffer.Length;
                using (Stream stream = wc.GetRequestStream())
                {
                    stream.Write(postBuffer, 0, postBuffer.Length);
                    stream.Flush();
                    stream.Close();
                }
            }
            
            using (HttpWebResponse res = (HttpWebResponse)wc.GetResponse())//处理接收
            {
                Stream response = res.GetResponseStream();
                if (wc.CookieContainer != null)
                {
                    res.Cookies = wc.CookieContainer.GetCookies(wc.RequestUri);
                }
                string responseUrl = res.ResponseUri.ToString();
                StreamReader sr = new StreamReader(response);
                string c = sr.ReadToEnd();
                Console.WriteLine(c);
                return c;
            }
        }    public string GetVerifyCode(string sourceUrl, string saveVirtualAddress)//获取验证码
        {
            
            CookieContainer cc = new CookieContainer();
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sourceUrl);
            req.CookieContainer = cc;
            using (WebResponse res = req.GetResponse())
            {            System.Drawing.Image bmp = Bitmap.FromStream(res.GetResponseStream());
                string fn = System.IO.Path.Combine(SysConfig.ApplicationPath + saveVirtualAddress.TrimEnd('/') + "/", Guid.NewGuid().ToString() + ".jpg");
                string address = HttpContext.Current.Server.MapPath(fn);
                bmp.Save(address, ImageFormat.Jpeg);
        
                return fn;
            }
            
        }源码。。cc一直未空
      

  2.   

    你要自己解析验证码啊?
    request.CookieContainer = new CookieContainer();
    foreach (string ItemName in Request.Cookies.AllKeys)
    {
        request.CookieContainer.Add(new Cookie 
        {
            Name = Request.Cookies[ItemName].Name,
            Value = Request.Cookies[ItemName].Value,
            Domain = Request.Cookies[ItemName].Domain,
            HttpOnly = Request.Cookies[ItemName].HttpOnly,
            Expires = Request.Cookies[ItemName].Expires,
            Path = Request.Cookies[ItemName].Path 
        });
    }
      

  3.   

    没看到你给 cc 添加值
    wc.Method = "POST"; 改成 GET  
      

  4.   

    验证码是手动输入的,  NameValueCollection v = new NameValueCollection();
            v.Add("username", "xingch50");
            v.Add("password", "mygoogle");
            v.Add("checkNum", code);
            string ceshi = Post(url, v);