功能描述:
----1.获取指定的页面的验证码.
----2.根据验证码.将一些数据提交给网站的验证用户页面[如].得到返回结果.
问题描述:
获取指定页面验证码: #region 加载验证码
/// <summary>
/// 获取验证码图片
/// </summary>
public void LoadImage()
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream responseStream = null;
Stream fileStream = null;
string imgName = "code.gif"; if ( this.VerifyCode.Image != null )
{
this.VerifyCode.Image.Dispose();
this.VerifyCode.Image = null;
} request = ( HttpWebRequest ) WebRequest.Create( "http://..../login_code.aspx?" );
//request.Accept = "*/*";
request.Method = "GET";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.ContentType = "text/html";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)";
request.CookieContainer = myCookie; response = ( HttpWebResponse ) request.GetResponse();
myCookie.Add( response.Cookies );
header = response.Headers;
responseStream = response.GetResponseStream();
fileStream = File.Create( imgName ); byte[] bytes = new byte[ 1024 ];
int count = 0;
do
{
count = responseStream.Read( bytes , 0 , bytes.Length );
fileStream.Write( bytes , 0 , count );
}
while ( count > 0 ); request = null;
response = null;
fileStream.Close();
fileStream = null;
responseStream.Close();
responseStream = null;
//将获取的验证码保存成图片格式,并放在image控件里
this.VerifyCode.Image = Image.FromFile( imgName );
}
#endregion提交数据给网站的验证用户:#region LOGIN事件
/// <summary>
/// login事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmLogin_Click( object sender , EventArgs e )
{
txtInfo.Text += "开始登录。\r\n";
try
{
//postData1是要发送的数据,如用户名,密码等信息
string postData1 = ibcnet.postData( txtVerifyCode.Text );
txtInfo.Text += "----需要发送的数据 :" + "\r\n" + postData1 + "\r\n";
//将数据发送给指定的页面
HttpWebRequest request1 = ( HttpWebRequest ) WebRequest.Create( "http://.../ProcessLogin.aspx" );
request1.Method = "POST";
request1.KeepAlive = true;
request1.AllowAutoRedirect = false;
request1.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)";
request1.ContentType = "application/x-www-form-urlencoded"; request1.CookieContainer = myCookie; Byte[] bytes = Encoding.ASCII.GetBytes( postData1 ); int streamLength = bytes.Length;
txtInfo.Text += "----发送数据的长度:" + streamLength + "\r\n";
request1.ContentLength = streamLength;
Stream stream = request1.GetRequestStream();
stream.Write( bytes , 0 , streamLength );
stream.Close(); HttpWebResponse response1 = null;
response1 = ( HttpWebResponse ) request1.GetResponse(); // _cookieContainer = request1.CookieContainer; StreamReader streamReader = new StreamReader( response1.GetResponseStream() , Encoding.UTF8 );
string outinfo = streamReader.ReadToEnd();
int xx = outinfo.IndexOf( "You inputed an invalid code" );
txtInfo.Text += "----得到的返回信息:" + "\r\n" + outinfo + "\r\n";
streamReader.Close(); }
catch ( Exception ex )
{
txtInfo.Text += "----发送数据发生异常:" + "\r\n" + ex.Message + "\r\n";
} }
#endregion
在上面的代码中可以正确的获取到验证码,但是该网站在验证用户的过程中,每登录一次网站,会产生一个值ASP.NET_SessionId,当我get[获取验证码]数据到http://..../login_code.aspx?时就得到了这个值,但是当post[验证用户]数据到http://.../ProcessLogin.aspx时,由于重开了一个HttpWebRequest
数据丢失.所以验证会产生错误.问题:
----怎样共享二个HttpWebRequest对象?
----或是有什么更好的方法处理这些问题.谢谢各位.
----1.获取指定的页面的验证码.
----2.根据验证码.将一些数据提交给网站的验证用户页面[如].得到返回结果.
问题描述:
获取指定页面验证码: #region 加载验证码
/// <summary>
/// 获取验证码图片
/// </summary>
public void LoadImage()
{
HttpWebRequest request = null;
HttpWebResponse response = null;
Stream responseStream = null;
Stream fileStream = null;
string imgName = "code.gif"; if ( this.VerifyCode.Image != null )
{
this.VerifyCode.Image.Dispose();
this.VerifyCode.Image = null;
} request = ( HttpWebRequest ) WebRequest.Create( "http://..../login_code.aspx?" );
//request.Accept = "*/*";
request.Method = "GET";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.ContentType = "text/html";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)";
request.CookieContainer = myCookie; response = ( HttpWebResponse ) request.GetResponse();
myCookie.Add( response.Cookies );
header = response.Headers;
responseStream = response.GetResponseStream();
fileStream = File.Create( imgName ); byte[] bytes = new byte[ 1024 ];
int count = 0;
do
{
count = responseStream.Read( bytes , 0 , bytes.Length );
fileStream.Write( bytes , 0 , count );
}
while ( count > 0 ); request = null;
response = null;
fileStream.Close();
fileStream = null;
responseStream.Close();
responseStream = null;
//将获取的验证码保存成图片格式,并放在image控件里
this.VerifyCode.Image = Image.FromFile( imgName );
}
#endregion提交数据给网站的验证用户:#region LOGIN事件
/// <summary>
/// login事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmLogin_Click( object sender , EventArgs e )
{
txtInfo.Text += "开始登录。\r\n";
try
{
//postData1是要发送的数据,如用户名,密码等信息
string postData1 = ibcnet.postData( txtVerifyCode.Text );
txtInfo.Text += "----需要发送的数据 :" + "\r\n" + postData1 + "\r\n";
//将数据发送给指定的页面
HttpWebRequest request1 = ( HttpWebRequest ) WebRequest.Create( "http://.../ProcessLogin.aspx" );
request1.Method = "POST";
request1.KeepAlive = true;
request1.AllowAutoRedirect = false;
request1.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)";
request1.ContentType = "application/x-www-form-urlencoded"; request1.CookieContainer = myCookie; Byte[] bytes = Encoding.ASCII.GetBytes( postData1 ); int streamLength = bytes.Length;
txtInfo.Text += "----发送数据的长度:" + streamLength + "\r\n";
request1.ContentLength = streamLength;
Stream stream = request1.GetRequestStream();
stream.Write( bytes , 0 , streamLength );
stream.Close(); HttpWebResponse response1 = null;
response1 = ( HttpWebResponse ) request1.GetResponse(); // _cookieContainer = request1.CookieContainer; StreamReader streamReader = new StreamReader( response1.GetResponseStream() , Encoding.UTF8 );
string outinfo = streamReader.ReadToEnd();
int xx = outinfo.IndexOf( "You inputed an invalid code" );
txtInfo.Text += "----得到的返回信息:" + "\r\n" + outinfo + "\r\n";
streamReader.Close(); }
catch ( Exception ex )
{
txtInfo.Text += "----发送数据发生异常:" + "\r\n" + ex.Message + "\r\n";
} }
#endregion
在上面的代码中可以正确的获取到验证码,但是该网站在验证用户的过程中,每登录一次网站,会产生一个值ASP.NET_SessionId,当我get[获取验证码]数据到http://..../login_code.aspx?时就得到了这个值,但是当post[验证用户]数据到http://.../ProcessLogin.aspx时,由于重开了一个HttpWebRequest
数据丢失.所以验证会产生错误.问题:
----怎样共享二个HttpWebRequest对象?
----或是有什么更好的方法处理这些问题.谢谢各位.
解决方案 »
- 问大家一个问题?
- 怎么做出拼图的那种效果?
- 请问一个控件--》显示html
- Script代码翻译成C#语言代码
- 急!!!关于类型转换的问题
- 不使用sqlDataAdapter适配器和dataSet数据集,用sqlDataReader能不能直接将sql Server2000中……
- xml xsd校验问题
- 找到bug者50到100分,即开即结!打算花3000分!
- 似乎没有人能够解决的问题:用CreateElement创建元素时多出的xmlns=""怎么去掉?
- DropDownList组合框绑定问题
- .net如何在Excel中如何在B1,B5列之间画一个矩形
- gridview 的HyperLinkField参数问题
结贴