我是新手想做一个登入
我老获取不到验证码
高手帮帮忙啊!!!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.Collections;
using System.IO;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
CookieContainer cookie = new CookieContainer();
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
GetHtmlData("http://www.doubao.com/user/login", cookie);
DowloadCheckImg("http://www.doubao.com/auth/checkcode/",cookie,@"C:\111.png"); }
/// <summary>
/// 通过get方式请求页面,传递一个实例化的cookieContainer
/// </summary>
/// <param name="postUrl"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
{
HttpWebRequest request;
HttpWebResponse response;
ArrayList list = new ArrayList();
request = WebRequest.Create(postUrl) as HttpWebRequest;
request.Method = "GET";
request.UserAgent = "Mozilla/4.0";
request.CookieContainer = cookie;
request.KeepAlive = true; request.CookieContainer = cookie;
try
{
//获取服务器返回的资源
using (response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
{
cookie.Add(response.Cookies);
//保存Cookies
list.Add(cookie);
list.Add(reader.ReadToEnd());
list.Add(Guid.NewGuid().ToString());//图片名
}
}
}
catch (WebException ex)
{
list.Clear();
list.Add("发生异常/n/r");
WebResponse wr = ex.Response;
using (Stream st = wr.GetResponseStream())
{
using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
{
list.Add(sr.ReadToEnd());
}
}
}
catch (Exception ex)
{
list.Clear();
list.Add("5");
list.Add("发生异常:" + ex.Message);
}
return list;
}
/// <summary>
/// 下载验证码图片并保存到本地
/// </summary>
/// <param name="Url">验证码URL</param>
/// <param name="cookCon">Cookies值</param>
/// <param name="savePath">保存位置/文件名</param>
public static bool DowloadCheckImg(string Url, CookieContainer cookCon, string savePath)
{
bool bol = true;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
//属性配置
webRequest.AllowWriteStreamBuffering = true;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.MaximumResponseHeadersLength = -1;
webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; (R1 1.5); .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET4.0C; .NET4.0E; Alexa Toolbar; u9dnfsh)";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "GET";
webRequest.Headers.Add("Accept-Language", "zh-cn");
webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
webRequest.KeepAlive = true;
webRequest.CookieContainer = cookCon;
try
{
//获取服务器返回的资源
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (Stream sream = webResponse.GetResponseStream())
{
List<byte> list = new List<byte>();
while (true)
{
int data = sream.ReadByte();
if (data == -1)
break;
list.Add((byte)data);
}
File.WriteAllBytes(savePath, list.ToArray());
}
}
}
catch (WebException ex)
{
bol = false;
}
catch (Exception ex)
{
bol = false;
}
return bol;
}
}
}
我老获取不到验证码
高手帮帮忙啊!!!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.Collections;
using System.IO;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
CookieContainer cookie = new CookieContainer();
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
GetHtmlData("http://www.doubao.com/user/login", cookie);
DowloadCheckImg("http://www.doubao.com/auth/checkcode/",cookie,@"C:\111.png"); }
/// <summary>
/// 通过get方式请求页面,传递一个实例化的cookieContainer
/// </summary>
/// <param name="postUrl"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
{
HttpWebRequest request;
HttpWebResponse response;
ArrayList list = new ArrayList();
request = WebRequest.Create(postUrl) as HttpWebRequest;
request.Method = "GET";
request.UserAgent = "Mozilla/4.0";
request.CookieContainer = cookie;
request.KeepAlive = true; request.CookieContainer = cookie;
try
{
//获取服务器返回的资源
using (response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
{
cookie.Add(response.Cookies);
//保存Cookies
list.Add(cookie);
list.Add(reader.ReadToEnd());
list.Add(Guid.NewGuid().ToString());//图片名
}
}
}
catch (WebException ex)
{
list.Clear();
list.Add("发生异常/n/r");
WebResponse wr = ex.Response;
using (Stream st = wr.GetResponseStream())
{
using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
{
list.Add(sr.ReadToEnd());
}
}
}
catch (Exception ex)
{
list.Clear();
list.Add("5");
list.Add("发生异常:" + ex.Message);
}
return list;
}
/// <summary>
/// 下载验证码图片并保存到本地
/// </summary>
/// <param name="Url">验证码URL</param>
/// <param name="cookCon">Cookies值</param>
/// <param name="savePath">保存位置/文件名</param>
public static bool DowloadCheckImg(string Url, CookieContainer cookCon, string savePath)
{
bool bol = true;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
//属性配置
webRequest.AllowWriteStreamBuffering = true;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.MaximumResponseHeadersLength = -1;
webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; (R1 1.5); .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET4.0C; .NET4.0E; Alexa Toolbar; u9dnfsh)";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "GET";
webRequest.Headers.Add("Accept-Language", "zh-cn");
webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
webRequest.KeepAlive = true;
webRequest.CookieContainer = cookCon;
try
{
//获取服务器返回的资源
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (Stream sream = webResponse.GetResponseStream())
{
List<byte> list = new List<byte>();
while (true)
{
int data = sream.ReadByte();
if (data == -1)
break;
list.Add((byte)data);
}
File.WriteAllBytes(savePath, list.ToArray());
}
}
}
catch (WebException ex)
{
bol = false;
}
catch (Exception ex)
{
bol = false;
}
return bol;
}
}
}
这个就是验证码图片地址,后面的0.****是 Math.random() 随机生成的把你这个【DowloadCheckImg("http://www.doubao.com/auth/checkcode/"】
换成上面的