请问如何通过ASP.NET 模拟POST http://bjs.ht380.com.cn/Login2.aspx我想些个 代码 模拟登录这个网站但是上面有个验证码。我想 先取得上面的图片然后将验证码用户名密码一起提交。实现登录。求高手指点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考 ..NET 软件自动化测试 一书。网上有源码,老外写的。 string url = " http://localhost:1925/WebForm1.aspx"; string data = "TextBox1=123&TextBox2=bbbbbbbb&Button1=Button"; string vs = "/wEPDwULLTE1OTg1NDYyNDZkZCGOa/w0BH7Cs9LGH2qS3FL67tzp"; string ev = "/wEWBAK18471BwLs0bLrBgKM54rGBgLs0fbZDHqUBuWM1RQZNNSGQxX0Vz3+amFh"; vs = HttpUtility.UrlEncode(vs); ev = HttpUtility.UrlEncode(ev); data += "&__VIEWSTATE=" + vs + "&__EVENTVALIDATION=" + ev; CookieContainer cook = new CookieContainer(); string HTML = getHTMLByUrlCook(url, ref cook, "POST", data, true, System.Text.Encoding.UTF8); Console.Write(HTML); public string getHTMLByUrlCook(string url, ref System.Net.CookieContainer cook, string sMethod, string Param, bool bAutoRedirect, System.Text.Encoding ecode) { sMethod = sMethod.ToUpper(); sMethod = sMethod != "POST" ? "GET" : sMethod; string res = ""; HttpWebRequest re = (HttpWebRequest)HttpWebRequest.Create(url); re.CookieContainer = cook; // attach the cook object re.Method = sMethod; re.AllowAutoRedirect = bAutoRedirect; re.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; MyIE2; .NET CLR 1.1.4322)"; re.Referer = url; if (sMethod == "POST") // Post data to Server { re.ContentType = "application/x-www-form-urlencoded"; Byte[] b = ecode.GetBytes(Param); re.ContentLength = b.Length; try { Stream oSRe = re.GetRequestStream(); oSRe.Write(b, 0, b.Length); oSRe.Close(); oSRe = null; } catch (Exception) { re = null; return "-1"; } } HttpWebResponse rep = null; Stream oResponseStream = null; StreamReader oSReader = null; try { rep = (HttpWebResponse)re.GetResponse(); oResponseStream = rep.GetResponseStream(); oSReader = new StreamReader(oResponseStream, ecode); res = oSReader.ReadToEnd(); } catch (System.Net.WebException e) { //res ="-1"; res = e.ToString(); } if (rep != null) { rep.Close(); rep = null; } if (oResponseStream != null) { oResponseStream.Close(); oResponseStream = null; } if (oSReader != null) { oSReader.Close(); oSReader = null; } re = null; return res; } 验证码比较难获得,即使获取了验证码,也不好把值赋值给session,因为session是服务器端的,期待高手。这里有一个post数据的例子,供参考! public static string GetWebContent(string _firstname, string _lastName, string _sex, string _year, string _month, string _day) { string html = ""; try { //-------------------------------------------------------------------------------------------------------------- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://sina.sm.aqioo.com/XingMingPingFen.html?"); byte[] data = Encoding.UTF8.GetBytes(string.Format("txtMFirstName={5}&txtMLastName={0}&sex1={1}&year1={2}&month1={3}&day1={4}", _lastName, _sex, _year, _month, _day, _firstname)); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; Stream writer = request.GetRequestStream(); writer.Write(data, 0, data.Length); writer.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); html = reader.ReadToEnd(); reader.Close(); response.Close(); return html; } catch { return ("出错"); } } 求一个有效的正则表达式 url重写问题在线等,急急急.......... 导出excel时,纯数字过长,被转义。添加半角的"'" webservice修改后如何使用 asp.net2.0+Sql2005超强的Grid(Ext),就是运行有点慢,但功能非常强!!!! 高分征求关于html在线编辑器控件的中文闪烁问题 如何隐藏repeater控件的特定一个列? 将参数值从 String 转换到 Int32 失败。 通过IE下载配置文件的问题,高分相酬,不够再加,先谢了 ASP.NET中有下拉时间控件吗,如Winform中的时间控件, 急求一个正则表达式 100028755程序员技术群欢迎加入..
网上有源码,老外写的。
string data = "TextBox1=123&TextBox2=bbbbbbbb&Button1=Button"; string vs = "/wEPDwULLTE1OTg1NDYyNDZkZCGOa/w0BH7Cs9LGH2qS3FL67tzp";
string ev = "/wEWBAK18471BwLs0bLrBgKM54rGBgLs0fbZDHqUBuWM1RQZNNSGQxX0Vz3+amFh";
vs = HttpUtility.UrlEncode(vs);
ev = HttpUtility.UrlEncode(ev); data += "&__VIEWSTATE=" + vs + "&__EVENTVALIDATION=" + ev; CookieContainer cook = new CookieContainer();
string HTML = getHTMLByUrlCook(url, ref cook, "POST", data, true, System.Text.Encoding.UTF8);
Console.Write(HTML); public string getHTMLByUrlCook(string url, ref System.Net.CookieContainer cook, string sMethod, string Param, bool bAutoRedirect, System.Text.Encoding ecode)
{
sMethod = sMethod.ToUpper();
sMethod = sMethod != "POST" ? "GET" : sMethod;
string res = "";
HttpWebRequest re = (HttpWebRequest)HttpWebRequest.Create(url);
re.CookieContainer = cook; // attach the cook object
re.Method = sMethod;
re.AllowAutoRedirect = bAutoRedirect;
re.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; MyIE2; .NET CLR 1.1.4322)";
re.Referer = url;
if (sMethod == "POST") // Post data to Server
{
re.ContentType = "application/x-www-form-urlencoded";
Byte[] b = ecode.GetBytes(Param);
re.ContentLength = b.Length;
try
{
Stream oSRe = re.GetRequestStream();
oSRe.Write(b, 0, b.Length);
oSRe.Close();
oSRe = null;
}
catch (Exception)
{
re = null;
return "-1";
}
} HttpWebResponse rep = null;
Stream oResponseStream = null;
StreamReader oSReader = null;
try
{
rep = (HttpWebResponse)re.GetResponse(); oResponseStream = rep.GetResponseStream();
oSReader = new StreamReader(oResponseStream, ecode);
res = oSReader.ReadToEnd();
}
catch (System.Net.WebException e)
{
//res ="-1"; res = e.ToString();
} if (rep != null)
{
rep.Close();
rep = null;
}
if (oResponseStream != null)
{
oResponseStream.Close();
oResponseStream = null;
} if (oSReader != null)
{
oSReader.Close();
oSReader = null;
}
re = null; return res; }
{
string html = "";
try
{
//--------------------------------------------------------------------------------------------------------------
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://sina.sm.aqioo.com/XingMingPingFen.html?");
byte[] data = Encoding.UTF8.GetBytes(string.Format("txtMFirstName={5}&txtMLastName={0}&sex1={1}&year1={2}&month1={3}&day1={4}", _lastName, _sex, _year, _month, _day, _firstname)); request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length; Stream writer = request.GetRequestStream();
writer.Write(data, 0, data.Length);
writer.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
html = reader.ReadToEnd(); reader.Close();
response.Close();
return html;
}
catch
{
return ("出错");
}
}