日期:2014-05-17 浏览次数:20419 次
using System;
using System.Data;
using System.Configuration;
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.Collections;
using System.Text;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
public static string PostAndGetHTML(string targetURL, CookieContainer cc, Hashtable param)
{
//prepare the submit data
string formData = "";
foreach (DictionaryEntry de in param)
{
formData += de.Key.ToString() + "=" + de.Value.ToString() + "&";
}
if (formData.Length > 0)
formData = formData.Substring(0, formData.Length - 1); //remove last '&'
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(formData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
request.Method = "POST"; //post
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
return result;
}
protected void Button1_Click(object sender, EventArgs e)
{
CookieContainer cc = new CookieContainer();//this is for keep th