转帖地址:http://www.cnblogs.com/Fooo/archive/2010/10/13/1850179.html
排除浏览器等其他因素,只考虑程序的问题,
基本上是因为没在 HttpWebRequest.GetResponse 之前先对 request 的 CookieContainer 实例化
简单代码如下:
如果没在 GetResponse 之前实例化 myRequest.CookieContainer,
访问 response.Cookies , 都是空的。
//**********************************
代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace MatchWin
{
/// <summary>
/// Http操作类
/// </summary>
public static class httptest
{
/// <summary>
/// 获取网址HTML
/// </summary>
/// <param name="URL"> 网址 </param>
/// <returns> </returns>
public static string GetHtml( string URL)
{
WebRequest wrt;
wrt = WebRequest.Create(URL);
wrt.Credentials = CredentialCache.DefaultCredentials;
WebResponse wrp;
wrp = wrt.GetResponse();
string reader = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding( " gb2312 " )).ReadToEnd();
try
{
wrt.GetResponse().Close();
}
catch (WebException ex)
{
throw ex;
}
return reader;
}
/// <summary>
/// 获取网站cookie
/// </summary>
/// <param name="URL"> 网址 </param>
/// <param name="cookie"> cookie </param>
/// <returns> </returns>
public static string GetHtml( string URL, out string cookie)
{
WebRequest wrt;
wrt = WebRequest.Create(URL);
wrt.Credentials = CredentialCache.DefaultCredentials;
WebResponse wrp;
wrp = wrt.GetResponse();
string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding( " gb2312 " )).ReadToEnd();
try
{
wrt.GetResponse().Close();
}
catch (WebException ex)
{
throw ex;
}
cookie = wrp.Headers.Get( " Set-Cookie " );
return html;
}
public static string GetHtml( string URL, string postData, string cookie, out string header, string server)
{
return GetHtml(server, URL, postData, cookie, out header);
}
public static string GetHtml( string server, string URL, string postData, string cookie, out string header)
{
byte [] byteRequest = Encoding.GetEncoding( " gb2312 " ).GetBytes(postData);
return GetHtml(server, URL, byteRequest, cookie, out header);
}
/// <summary>
/// C# :从一段字符串中,输入开始和结束的字符,取中间的字符
/// </summary>
/// <param name="str"> 一段字符串 </param>
/// <param name="strStart"> 开始字符 </param>
/// <param name="strEnd"> 结束字符 </param>
/// <returns></returns>
public static string AnalyzeMessage( string str, string strStart, string strEnd)
{
string Result = "" ;
int i = str.IndexOf(strStart);
if (i >= 0 )
{
int j = str.IndexOf(strEnd, i + strStart.Length);
if (j > 0 )
{
Result = str.Substring(i + strStart.Length, j - i - strStart.Length);
}
}
return Result;
}
/// <summary>
/// 与请求相关的cookie(用于保持session)
/// </summary>
public static CookieContainer LoginCookies = new CookieContainer();
public static CookieCollection gCookieCollention = new CookieCollection();
// Cookies集合保存
public static CookieCollection CCol = new CookieCollection();
public static string GetHtml222( string URL, string cookie)
{
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
StreamReader streamReader;
string getString = "" ;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
httpWebRequest.ContentType = " application/x-www-form-urlencoded " ;
httpWebRequest.Accept =
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* " ;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
// 自动跳转。。。
httpWebRequest.AllowAutoRedirect = false ;
httpWebRequest.KeepAlive = true ;
httpWebRequest.CookieContainer = LoginCookies;
httpWebRequest.Method = " GET " ;
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.GetEncoding( " gb2312 " )); // Encoding.GetEncoding("gb2312") Encoding.GetEncoding("utf-8")
getString = streamReader.ReadToEnd();
// LoginCookies = httpWebRequest.CookieContainer;
CCol = LoginCookies.GetCookies(httpWebRequest.RequestUri);
// string ksfd = CCol[0].Name + CCol[0].Value;
streamReader.Close();
getStream.Close();
// *********************************获取cookie
Regex RegExFindHref = new Regex( @" <a/s+([^>]*/s*)?href/s*=/s*(?:""(?<1>[//a-z0-9_][^""]*)""|'(?<1>[//a-z0-9_][^']*)'
|(?<1>[//a-z0-9_]/S*))(/s[^>]*)?>(?<2>.*?)</a> " , RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
string ht = "" ;
for (Match m = RegExFindHref.Match(getString); m.Success; m = m.NextMatch())
{
ht = " http://agt.ibc88.com " + System.Web.HttpUtility.UrlDecode(m.Groups[ 1 ].ToString());
}
ht = ht.Replace( " amp; " , "" );
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(ht);
/// /CookieContainer co = new CookieContainer();
httpWebRequest.CookieContainer = new CookieContainer();
// httpWebRequest.CookieContainer.Add(new Uri(ht), "");
httpWebRequest.ContentType = " application/x-www-form-urlencoded " ;
httpWebRequest.Accept =
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* " ;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
httpWebRequest.Method = " Get " ;
// 自动跳转。。。
httpWebRequest.Referer = URL;
httpWebRequest.AllowAutoRedirect = false ;
httpWebRequest.KeepAlive = true ;
httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
// httpWebRequest.CookieContainer = LoginCookies;
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.GetEncoding( " gb2312 " ));
getString = streamReader.ReadToEnd();
cookie = webResponse.Headers.Get( " Set-Cookie " );
streamReader.Close();
getStream.Close();
// *********************************************************************************************
return getString;
}
public static string GetHtml555( string URL, string cookie)
{
// WebRequest wrt;
// wrt = WebRequest.Create(URL);
// wrt.Credentials = CredentialCache.DefaultCredentials;
// WebResponse wrp;
// wrp = wrt.GetResponse();
// string html = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gb2312")).ReadToEnd();
// try
// {
// wrt.GetResponse().Close();
// }
// catch (WebException ex)
// {
// throw ex;
// }
// cookie = wrp.Headers.Get("Set-Cookie");
// ***********************************************************************
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
StreamReader streamReader;
string getString = "" ;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
// CookieContainer co = new CookieContainer();
// co.SetCookies(new Uri(" https://ag.ibc88.com/ "), cookie);
httpWebRequest.ContentType = " application/x-www-form-urlencoded " ;
httpWebRequest.Accept =
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* " ;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
httpWebRequest.Method = " Get " ;
// 自动跳转。。。
httpWebRequest.AllowAutoRedirect = false ;
httpWebRequest.KeepAlive = true ;
httpWebRequest.CookieContainer = LoginCookies;
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.GetEncoding( " gb2312 " ));
getString = streamReader.ReadToEnd();
// cookie = webResponse.Headers.Get("Set-Cookie");
streamReader.Close();
getStream.Close();
return getString;
}
public static string GetHtml333( string URL, string cookie, string urlReferer)
{
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
StreamReader streamReader;
string getString = "" ;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
httpWebRequest.ContentType = " application/x-www-form-urlencoded " ;
httpWebRequest.Accept =
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* " ;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
httpWebRequest.Method = " Get " ;
// 自动跳转。。。
httpWebRequest.AllowAutoRedirect = false ;
httpWebRequest.KeepAlive = true ;
httpWebRequest.CookieContainer = LoginCookies;
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.UTF8);
getString = streamReader.ReadToEnd();
streamReader.Close();
getStream.Close();
return getString;
}
public static string GetHtml( string server, string URL, byte [] byteRequest, string cookie, out string header)
{
long contentLength;
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
StreamReader streamReader;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
LoginCookies.SetCookies( new Uri(server), cookie);
httpWebRequest.CookieContainer = LoginCookies;
httpWebRequest.ContentType = " application/x-www-form-urlencoded " ;
httpWebRequest.Accept =
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* " ;
httpWebRequest.Referer = server;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
httpWebRequest.Method = " Post " ;
httpWebRequest.AllowAutoRedirect = false ;
httpWebRequest.KeepAlive = true ;
httpWebRequest.ContentLength = byteRequest.Length;
Stream stream;
stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0 , byteRequest.Length);
stream.Close();
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
int _APSNET_SessionValue = webResponse.Cookies.Count;
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
contentLength = webResponse.ContentLength;
byte [] outBytes = new byte [contentLength];
outBytes = ReadFully(getStream);
getStream.Close();
getStream = new MemoryStream(outBytes);
streamReader = new StreamReader(getStream, Encoding.GetEncoding( " gb2312 " ));
string getString = streamReader.ReadToEnd();
streamReader.Close();
getStream.Close();
// 第二段*********************
Regex RegExFindHref = new Regex( @" <a/s+([^>]*/s*)?href/s*=/s*(?:""(?<1>[//a-z0-9_][^""]*)""|'(?<1>[//a-z0-9_][^']*)'
|(?<1>[//a-z0-9_]/S*))(/s[^>]*)?>(?<2>.*?)</a> " , RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
string TwoStr = "" ;
for (Match m = RegExFindHref.Match(getString); m.Success; m = m.NextMatch())
{
TwoStr = m.Groups[ 1 ].ToString();
}
getString = "" ;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(TwoStr);
httpWebRequest.ContentType = " application/x-www-form-urlencoded " ;
httpWebRequest.Accept =
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* " ;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
// 自动跳转。。。
httpWebRequest.AllowAutoRedirect = false ;
httpWebRequest.KeepAlive = true ;
httpWebRequest.CookieContainer = LoginCookies;
httpWebRequest.Method = " GET " ;
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
_APSNET_SessionValue = webResponse.Cookies.Count;
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.GetEncoding( " gb2312 " )); // Encoding.GetEncoding("gb2312") Encoding.GetEncoding("utf-8")
getString = streamReader.ReadToEnd();
streamReader.Close();
getStream.Close();
// *********************************获取cookie
RegExFindHref = new Regex( @" <a/s+([^>]*/s*)?href/s*=/s*(?:""(?<1>[//a-z0-9_][^""]*)""|'(?<1>[//a-z0-9_][^']*)'
|(?<1>[//a-z0-9_]/S*))(/s[^>]*)?>(?<2>.*?)</a> " , RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
string ht = "" ;
for (Match m = RegExFindHref.Match(getString); m.Success; m = m.NextMatch())
{
ht = " http://agt.ibc88.com " + System.Web.HttpUtility.UrlDecode(m.Groups[ 1 ].ToString());
}
ht = ht.Replace( " amp; " , "" );
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(ht);
httpWebRequest.ContentType = " application/x-www-form-urlencoded " ;
httpWebRequest.Accept =
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* " ;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
httpWebRequest.Method = " Get " ;
// 自动跳转。。。
httpWebRequest.AllowAutoRedirect = false ;
httpWebRequest.KeepAlive = true ;
httpWebRequest.CookieContainer = LoginCookies;
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
_APSNET_SessionValue = webResponse.Cookies.Count;
string sdfff = webResponse.Cookies[ 0 ].Name + " : " + webResponse.Cookies[ 0 ].Value;
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.GetEncoding( " gb2312 " ));
getString = streamReader.ReadToEnd();
cookie = webResponse.Headers.Get( " Set-Cookie " );
LoginCookies.SetCookies(httpWebRequest.RequestUri, " rpt_bold=1; " + cookie);
streamReader.Close();
getStream.Close();
return getString;
}
/// <summary>
/// Post模式浏览
/// </summary>
/// <param name="server"> 服务器地址 </param>
/// <param name="URL"> 网址 </param>
/// <param name="byteRequest"> 流 </param>
/// <param name="cookie"> cookie </param>
/// <param name="header"> 句柄 </param>
/// <returns> </returns>
public static byte [] GetHtmlByBytes( string server, string URL, byte [] byteRequest, string cookie, out string header)
{
long contentLength;
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
StreamReader streamReader;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
// CookieContainer co = new CookieContainer();
// co.SetCookies(new Uri(server), cookie);
LoginCookies.SetCookies( new Uri(server), cookie);
httpWebRequest.CookieContainer = LoginCookies;
LoginCookies = httpWebRequest.CookieContainer;
httpWebRequest.ContentType = " application/x-www-form-urlencoded " ;
httpWebRequest.Accept =
" image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* " ;
httpWebRequest.Referer = server;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
httpWebRequest.Method = " Post " ;
httpWebRequest.AllowAutoRedirect = false ;
httpWebRequest.ContentLength = byteRequest.Length;
Stream stream;
stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0 , byteRequest.Length);
stream.Close();
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
contentLength = webResponse.ContentLength;
byte [] outBytes = new byte [contentLength];
outBytes = ReadFully(getStream);
getStream.Close();
return outBytes;
}
public static byte [] ReadFully(Stream stream)
{
byte [] buffer = new byte [ 128 ];
using (MemoryStream ms = new MemoryStream())
{
while ( true )
{
int read = stream.Read(buffer, 0 , buffer.Length);
if (read <= 0 )
return ms.ToArray();
ms.Write(buffer, 0 , read);
}
}
}
/// <summary>
/// Get模式
/// </summary>
/// <param name="URL"> 网址 </param>
/// <param name="cookie"> cookies </param>
/// <param name="header"> 句柄 </param>
/// <param name="server"> 服务器 </param>
/// <param name="val"> 服务器 </param>
/// <returns> </returns>
public static string GetHtml( string URL, string cookie, out string header, string server)
{
return GetHtml(URL, cookie, out header, server, "" );
}
/// <summary>
/// Get模式浏览
/// </summary>
/// <param name="URL"> Get网址 </param>
/// <param name="cookie"> cookie </param>
/// <param name="header"> 句柄 </param>
/// <param name="server"> 服务器地址 </param>
/// <param name="val"> </param>
/// <returns> </returns>
public static string GetHtml( string URL, string cookie, out string header, string server, string val)
{
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
StreamReader streamReader;
string getString = "" ;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
httpWebRequest.Accept = " */* " ;
httpWebRequest.Referer = server;
// CookieContainer co = new CookieContainer();
/// /co.SetCookies(new Uri(server), cookie);
// httpWebRequest.CookieContainer = co;
httpWebRequest.UserAgent =
" Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322) " ;
httpWebRequest.Method = " GET " ;
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
streamReader = new StreamReader(getStream, Encoding.GetEncoding( " gb2312 " ));
getString = streamReader.ReadToEnd();
streamReader.Close();
getStream.Close();
return getString;
}
/// <summary>
/// 返回验证码图片流
/// </summary>
/// <param name="server"> 服务器地址 </param>
/// <param name="URL"> 验证码网址 </param>
/// <param name="cookie"> cookie </param>
/// <returns> </returns>
public static Stream GetStreamByBytes( string server, string URL, string cookie)
{
Stream stream = GetCode(server, URL, cookie);
return stream;
}
/// <summary>
/// //获取验证码
/// </summary>
/// <param name="server"> 服务器地址 </param>
/// <param name="url"> 验证码网址 </param>
/// <param name="cookie"> cookie </param>
/// <returns> </returns>
public static Stream GetCode( string server, string url, string cookie)
{
HttpWebRequest httpWebRequest;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
CookieContainer co = new CookieContainer();
co.SetCookies( new Uri(server), cookie);
httpWebRequest.CookieContainer = co;
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = response.GetResponseStream();
return stream;
}
/// <summary>
/// 获取html
/// </summary>
/// <param name="server"> </param>
/// <param name="url"> </param>
/// <param name="cookie"> </param>
/// <returns> </returns>
public static string GetUser( string server, string url, string cookie)
{
string getString = "" ;
try
{
HttpWebRequest httpWebRequest;
StreamReader streamReader;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
CookieContainer co = new CookieContainer();
co.SetCookies( new Uri(server), cookie);
httpWebRequest.CookieContainer = co;
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = response.GetResponseStream();
streamReader = new StreamReader(stream, Encoding.GetEncoding( " gb2312 " ));
getString = streamReader.ReadToEnd();
try
{
httpWebRequest.GetResponse().Close();
}
catch (WebException ex)
{
throw ex;
}
streamReader.Close();
stream.Close();
}
catch
{
}
return getString;
}
}
}