if (this.bHandleCookies)
{
if (Response.Cookies.Count > 0)
{
if (this.oCookies == null)
{
this.oCookies = Response.Cookies;
}
else
{
// ** If we already have cookies update the list
foreach (Cookie oRespCookie in Response.Cookies)
{
bool bMatch = false;
foreach (Cookie oReqCookie in this.oCookies)
{
if (oReqCookie.Name == oRespCookie.Name)
{
oReqCookie.Value = oRespCookie.Value;
bMatch = true;
break; //
}
} // for each ReqCookies
if (!bMatch)
this.oCookies.Add(oRespCookie);
} // for each Response.Cookies
} // this.Cookies == null
} // if Response.Cookie.Count > 0
} // if this.bHandleCookies = 0
// *** Save the response object for external access
Encoding enc;
try
{
if (Response.ContentEncoding.Length > 0)
enc = Encoding.GetEncoding(Response.ContentEncoding);
else
enc = Encoding.GetEncoding(cEncoding);
}
catch
{
// *** Invalid encoding passed
enc = Encoding.GetEncoding(cEncoding);
}
// *** drag to a stream
StreamReader strResponse = new StreamReader(Response.GetResponseStream(), enc);
string str = strResponse.ReadToEnd();
Response.Close();
strResponse.Close();
//自动跟踪引用页
if (this.bHandleReferer)
{
this.cReferer = Url;
}
//自动处理HTTP/1.0 302 Moved Temporarily中的Location后的页面。(自动完成跳转)
if (this.bLocation)
{
using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Windows.Forms;//only For Use MessageBox
using System.Net;
using System.IO;
using System.Diagnostics;
namespace MyTools.NetWork.Http
{
public class MyHttp
{
/// <summary>
/// User name used for Authentication.
/// To use the currently logged in user when accessing an NTLM resource you can use "AUTOLOGIN".
/// </summary>
public string Username
{
get { return this.cUsername; }
set { cUsername = value; }
}
/// <summary>
/// Password for Authentication.
/// </summary>
public string Password
{
get { return this.cPassword; }
set { this.cPassword = value; }
}
/// <summary>
/// Address of the Proxy Server to be used.
/// Use optional DEFAULTPROXY value to specify that you want to IE's Proxy Settings
/// </summary>
public string ProxyAddress
{
get { return this.cProxyAddress; }
set { this.cProxyAddress = value; }
}
/// <summary>
/// Semicolon separated Address list of the servers the proxy is not used for.
/// </summary>
public string ProxyBypass
{
get { return this.cProxyBypass; }
set { this.cProxyBypass = value; }
}
/// <summary>
/// Username for a password validating Proxy. Only used if the proxy info is set.
/// </summary>
public string ProxyUsername
{
get { return this.cProxyUsername; }
set { this.cProxyUsername = value; }
}
/// <summary>
/// Password for a password validating Proxy. Only used if the proxy info is set.
/// </summary>
public string ProxyPassword
{
get { return this.cProxyPassword; }
set { this.cProxyPassword = value; }
}
/// <summary>
/// Timeout for the Web request in seconds. Times out on connection, read and send operations.
/// Default is 30 seconds.
/// </summary>
public int Timeout
{
get { return this.nConnectTimeout; }
set { this.nConnectTimeout = value; }
}
public bool HandleReferer
{
get { return this.bHandleReferer; }
set { this.bHandleReferer = value; }
}
/// <summary>
/// 引用页
/// </summary>
public string Referer
{
get { return this.cReferer; }
set { this.cReferer = value; }
}
/// <summary>
/// 提交模式,默认是POST,用GET模式的时候不能使用PostData
/// </summary>
/// <value></value>
public string Method
{
get { return this.cMethod; }
set { this.cMethod = value; }
}
/// <summary>
/// Error Message if the Error Flag is set or an error value is returned from a method.
/// </summary>
public string ErrorMsg
{
get { return this.cErrorMsg; }
set { this.cErrorMsg = value; }
}
/// <summary>
/// Error flag if an error occurred.
/// </summary>
public bool Error
{
get { return this.bError; }
set { this.bError = value; }
}
/// <summary>
/// Determines whether errors cause exceptions to be thrown. By default errors
/// are handled in the class and the Error property is set for error conditions.
/// (not implemented at this time).
/// </summary>
public bool ThrowExceptions
{
get { return bThrowExceptions; }
set { this.bThrowExceptions = value; }
}
/// <summary>
/// If set to a non-zero value will automatically track cookies. The number assigned is the cookie count.
/// </summary>
public bool HandleCookies
{
get { return this.bHandleCookies; }
set { this.bHandleCookies = value; }
}
//Cookies集合
public CookieCollection Cookies
{
get { return this.oCookies; }
set { this.Cookies = value; }
}
//默认的编码
public string MyEncoding
{
get { return this.cEncoding; }
set { this.cEncoding = value; }
}
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(),System.Text.Encoding.Default);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
response.Close();