You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.5 KiB
93 lines
2.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net;
|
|
|
|
namespace Stone.Common
|
|
{
|
|
public class MyHttpClient : WebClient
|
|
{
|
|
// Cookie 容器
|
|
private CookieContainer cookieContainer;
|
|
|
|
/**/
|
|
/// <summary>
|
|
/// 创建一个新的 WebClient 实例。
|
|
/// </summary>
|
|
public MyHttpClient()
|
|
{
|
|
this.cookieContainer = new CookieContainer();
|
|
}
|
|
|
|
/**/
|
|
/// <summary>
|
|
/// 创建一个新的 WebClient 实例。
|
|
/// </summary>
|
|
/// <param name="cookie">Cookie 容器</param>
|
|
public MyHttpClient(CookieContainer cookies)
|
|
{
|
|
this.cookieContainer = cookies;
|
|
}
|
|
|
|
/**/
|
|
/// <summary>
|
|
/// Cookie 容器
|
|
/// </summary>
|
|
public CookieContainer Cookies
|
|
{
|
|
get { return this.cookieContainer; }
|
|
set { this.cookieContainer = value; }
|
|
}
|
|
|
|
/**/
|
|
/// <summary>
|
|
/// 返回带有 Cookie 的 HttpWebRequest。
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|