网页登陆的问题

yangyinzhu 2008-07-14 04:48:36
这段代码为什么老是登陆不成功,说是验证码不正确,真要命了阿
Public class HttpRec
{
private CookieCollection _cookCollection;
private string _Url = "http://59.50.113.196:9080/personal/servlet/LoginServlet";
private string _refUrl = "http://59.50.113.196:9080/personal/servlet/LoginServlet";
private string _strErr;

public string GetCode()
{
HttpWebRequest rqq = (HttpWebRequest)HttpWebRequest.Create("Http://59.50.113.196:9080/security/servlet/AuthenCodeImage");
rqq.Method = "Get";
rqq.KeepAlive = true;
rqq.Referer = "http://59.50.113.196:9080/personal/interfaces/hainan/index.jsp";
if (rqq.CookieContainer == null)
{
rqq.CookieContainer = new CookieContainer();
}
HttpWebResponse rpp = (HttpWebResponse)rqq.GetResponse();
Image img = Bitmap.FromStream(rpp.GetResponseStream());
_cookCollection = rpp.Cookies;
// img.Save(Environment.CurrentDirectory+"\\1.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
return ChuLiImg((Bitmap)img);
}

public static string ChuLiImg(Bitmap img)
{
Rectangle[] RcArr = new Rectangle[4];
Rectangle RC1 = new Rectangle(4, 4, 6, 9);
Rectangle RC2 = new Rectangle(11, 4, 6, 9);
Rectangle RC3 = new Rectangle(18, 4, 6, 9);
Rectangle RC4 = new Rectangle(25, 4, 6, 9);
RcArr[0] = RC1;
RcArr[1] = RC2;
RcArr[2] = RC3;
RcArr[3] = RC4;
Bitmap[] bit = new Bitmap[4];
string CodeStr = string.Empty;
for (int i = 0; i < 4; i++)
{
string str =GetImgStr(ClearBg(img), RcArr[i]);
bit[i] = ((Bitmap)img).Clone(RcArr[i], img.PixelFormat);
if (str == "0111111111111111100000001000000011111111")
{
CodeStr += "0";
}
if (str == "0000000000110000011000001111111111111111")
{
CodeStr += "1";
}
if (str == "0100000111000011100001101000110011111000")
{
CodeStr += "2";
}
if (str == "0100000111000001100100001001000011111111")
{
CodeStr += "3";
}
if (str == "0000011000011010011000101111111111111111")
{
CodeStr += "4";
}
if (str == "0011100111111001110100001001000010011111")
{
CodeStr += "5";
}
if (str == "0011111101111111100100001001000011011111")
{
CodeStr += "6";
}
if (str == "1000000010000011100011111011110011110000")
{
CodeStr += "7";
}
if (str == "0111011111111111100010001000100011111111")
{
CodeStr += "8";
}
if (str == "0111100111111101100001001000010011111111")
{
CodeStr += "9";
}
}
return CodeStr;

}
public static string GetImgStr(Bitmap Bigimg, Rectangle Rc)
{
Bitmap RCBmp = Bigimg.Clone(Rc, Bigimg.PixelFormat);
string str = "";
for (int w = 0; w < Rc.Width - 1; w++)
{
for (int h = 0; h < Rc.Height - 1; h++)
{

if (RCBmp.GetPixel(w, h).ToArgb() == Color.Black.ToArgb())
{

str += "1";
}
else
{
str += "0";
}
}
}
return str;
}
private static Bitmap ClearBg(Image Img)
{
Bitmap Smallimg = new Bitmap(Img);
for (int w = 0; w < Smallimg.Width - 1; w++)
{
for (int h = 0; h < Smallimg.Height - 1; h++)
{

if (Smallimg.GetPixel(w, h).ToArgb() < (Color.Black.ToArgb() * 0.4))
{

Smallimg.SetPixel(w, h, Color.Black);
}
else
{
Smallimg.SetPixel(w, h, Color.White);
}
}
}
return Smallimg;
}

public string LoginWeb(string PostData)
{
string str=string.Empty;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(_Url);//创建req
req.Accept = "*/*"; //接受任意文件
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; // 模拟使用IE在浏览
// req.KeepAlive = true;
req.Referer = "http://59.50.113.196:9080/personal/interfaces/hainan/index.jsp";
if (req.CookieContainer == null)
{
req.CookieContainer = new CookieContainer();
}
req.CookieContainer.Add(_cookCollection);
if (PostData != null & PostData.Length > 0)
{
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] b = Encoding.Default.GetBytes(PostData);
System.IO.Stream sw = null;
try
{
sw = req.GetRequestStream();
sw.Write(b, 0, b.Length);
}
catch (System.Exception ex)
{
this._strErr = ex.Message;
}
finally
{
if (sw != null) { sw.Close(); }
}
}
HttpWebResponse rep = null;
System.IO.StreamReader sr = null;
try
{
rep = (HttpWebResponse)req.GetResponse();
_cookCollection = rep.Cookies;
sr = new System.IO.StreamReader(rep.GetResponseStream(), Encoding.Default);
str = sr.ReadToEnd();
if(sr!=null)
{
sr.Close();
}
}
catch(Exception e)
{ MessageBox.Show(e.Message); }

return str;
}
}
...全文
271 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzultc 2008-07-14
  • 打赏
  • 举报
回复
估计是访问index.jsp的时候,服务企业设置的也有cookies,你的程序没有得到。
你还是下在一个网络监控软件,查看一下,看看发送的cookies是否完全。
yangyinzhu 2008-07-14
  • 打赏
  • 举报
回复
不是阿,登陆的也有阿,登陆的LoginWeb 这个函数阿
grearo 2008-07-14
  • 打赏
  • 举报
回复
只有验证码的程序,怎么看?不一定是这里出错。估计你是在登陆页的未判断是否ispostback,登陆验证的时候又重新调用了验证码程序。

110,891

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧