读书人

求代码c#模拟登录带验证码的,该如何解

发布时间: 2013-09-05 16:02:07 作者: rapoo

求代码c#模拟登录带验证码的
求案例代码或者帮忙分析啊cookie一直为空,

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Drawing;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using LitJson;

/// <summary>
/// code 的摘要说明
/// </summary>
public class code
{
public code()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public string gethf(string phone, string pwd, string code)
{



string hidden="style=1&pwdType=2&service=my&continue=/my/login/loginSuccess.do&failurl=https://zj.ac.10086.cn/login";
string postdate ="passwd=" + pwd + "&validCode=" + code + "&billId="+phone+"&"+hidden;
string url = "http://oa.plusbe.com/Admin/Login.aspx?action=save&username=xingch50&password=mygoogle&checkNum=" + code + "";
string ceshi = Post(url, new NameValueCollection());
return ceshi;
}



private CookieContainer cc = new CookieContainer();

static System.Text.Encoding encoding = System.Text.Encoding.UTF8;
private 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/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";


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.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506; .NET CLR 3.5.21022)";
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, encoding);
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;
}
}


}


下面为调用代码
 code codes = new code();


if (this.Request["action"] == "save")
{
string chenck = this.Request["code"];
cath = codes.gethf("18268822438", "282157", chenck);
}
else
{
code = codes.GetVerifyCode("http://oa.plusbe.com/Admin/CodeImage.aspx", "verifycode");
}

模拟登录 验证码 c#模拟登录
[解决办法]
aspx代码:
<tr>
<td class="capitalField field" align="right">

</td>
<td class="field" colspan="2">
<input id="txt_CheckCode" type="text" runat="server" style="width: 146px;height:25px;" />
<div style="padding-top:3px;">
<img src="ValidateCode.aspx?type=RM" alt="求代码c#模拟登录带验证码的,该如何解决" id="ValidateCodeImg" />
<a href="javascript:changeValidateCodeImg();" style="color: Blue; cursor: pointer;


text-decoration: unerline">看不清,一</a>
</div>
</td>
</tr>




<script language="javascript" type="text/javascript">
function changeValidateCodeImg() {
document.getElementById('ValidateCodeImg').src = 'ValidateCode.aspx?type=RM&temp=' + (new Date().getTime().toString(36));
}
</script>


读书人网 >ASP

热点推荐