|
楼主 |
发表于 2014-10-30 11:06:59
|
显示全部楼层
本帖最后由 飞兔ss 于 2014-10-30 11:08 编辑
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(OpenReadWithHttps("http://XXX.COM/login", "user=admin&pass=admin"));
richTextBox1.Text = GetUrltoHtml("http://XXX.COM/main/");
}
///</summary>
///<param name="URL">url地址</param>
///<param name="strPostdata">发送的数据</param>
///<returns></returns>
public string OpenReadWithHttps(string URL, string strPostdata)
{
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "post";
request.Accept = "application/json, text/javascript, */*; q=0.01";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
CookieContainer objcok = new CookieContainer();
objcok.Add(new Uri("http://XXX.COM/"), new Cookie("PHPSESSID", "5ca7627089cs43f12f6e925a4fa31eee"));
request.CookieContainer = objcok;
byte[] buffer = encoding.GetBytes(strPostdata);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
return reader.ReadToEnd();
}
/// <summary>
/// 传入URL返回网页的html代码带有证书的方法
/// </summary>
/// <param name="Url">URL</param>
/// <returns></returns>
public string GetUrltoHtml(string Url)
{
StringBuilder content = new StringBuilder();
try
{
// 与指定URL创建HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR
2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; BOIE9;ZHCN)";
request.Method = "GET";
request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg,
application/x-ms-xbap, */*";
//如果方法验证网页来源就加上这一句如果不验证那就可以不写了
request.Referer = "http://XXX.COM/main/";
CookieContainer objcok = new CookieContainer();
objcok.Add(new Uri("http://XXX.COM/"), new Cookie("PHPSESSID", "5ca7627089cs43f12f6e925a4fa31eee"));
request.CookieContainer = objcok;
//不保持连接
request.KeepAlive = true;
// 获取对应HTTP请求的响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 获取响应流
Stream responseStream = response.GetResponseStream();
// 对接响应流(以"GBK"字符集)
StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
// 开始读取数据
Char[] sReaderBuffer = new Char[256];
int count = sReader.Read(sReaderBuffer, 0, 256);
while (count > 0)
{
String tempStr = new String(sReaderBuffer, 0, count);
content.Append(tempStr);
count = sReader.Read(sReaderBuffer, 0, 256);
}
// 读取结束
sReader.Close();
}
catch (Exception)
{
content = new StringBuilder("Runtime Error");
}
return content.ToString();
}
不太懂,你看看我这样做是否正确,是哪个地方需要修改。对COOKIE不懂。不知道如何下手。 |
|