62,047
社区成员
发帖
与我相关
我的任务
分享
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;
}