using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace Stone.Common
{
public class MyHttpClient : WebClient
{
// Cookie 容器
private CookieContainer cookieContainer;
/**/
///
/// 创建一个新的 WebClient 实例。
///
public MyHttpClient()
{
this.cookieContainer = new CookieContainer();
}
/**/
///
/// 创建一个新的 WebClient 实例。
///
/// Cookie 容器
public MyHttpClient(CookieContainer cookies)
{
this.cookieContainer = cookies;
}
/**/
///
/// Cookie 容器
///
public CookieContainer Cookies
{
get { return this.cookieContainer; }
set { this.cookieContainer = value; }
}
/**/
///
/// 返回带有 Cookie 的 HttpWebRequest。
///
///
///
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
HttpWebRequest httpRequest = request as HttpWebRequest;
httpRequest.CookieContainer = cookieContainer;
}
return request;
}
public string PostData(string uriString, string postString)
{
string result = "";
try
{
this.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] postData = Encoding.Default.GetBytes(postString);
byte[] responseData = this.UploadData(uriString, "POST", postData);
result = Encoding.Default.GetString(responseData);
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
public string GetData(string url)
{
string result = "";
try
{
result = System.Text.Encoding.Default.GetString(this.DownloadData(url));
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
}
}