復制代碼 代碼如下:
/****************************************************************
* 函數名稱:SendCommand(SendMethod method, ST_Param p)
* 功能說明:向遠程發送URL和參數,接受返回信息(無亂碼);
* 參 數:method:xml發送方法,POST/Get 兩種
P:參數結構體
public string Url; //遠程URL
public string Parameters; //參數
public string Uid; //帳號
public string Pwd; //號令
* 調用示列:
* using ebcnc; //引用空間名
* XMLHTTP x = new XMLHTTP(); //創建設xmlhttp對像
* XMLHTTP.ST_Param st = new XMLHTTP.ST_Param(); //創建參數數組
* st.Parameters = ""; //url詳細參數
* st.Url = "http://www.baidu.com/"; //url
* st.Uid = "" ; //帳號
* st.Pwd = ""; //口令
* string rn=””; //返回字符串
* rn=x.SendCommand(XMLHTTP.SendMethod.POST, st); //獲取返回信息
* x.Dispose();
***********************************************************************/
using System;
using MSXML2;
namespace ebcnc
{
/// <summary>
/// XMLHTTP基類
/// </summary>
public class XMLHTTP : IDisposable
{
#region 變量及參數
private XMLHTTPClass xml;
private bool _alreadyDispose = false;
public ST_Param Parameters;
public enum SendMethod : int
{
POST, GET
}
#endregion
#region 參數結構體
public struct ST_Param
{
public string Url;
public string Parameters;
public string Uid;
public string Pwd;
}
#endregion
#region 發送數據
/// <summary>
/// 發送數據
/// </summary>
/// <param name="method">發送方式</param>
/// <param name="p">數據</param>
/// <returns>STRING</returns>
public virtual string SendCommand(SendMethod method, ST_Param p)
{
if (p.Url == null || p.Url == "") return null;
if (method == SendMethod.POST)
{
try
{
xml.open("POST", p.Url, false, p.Uid, p.Pwd);
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.send(CommonFunction.UrlEncoding(p.Parameters));
if (xml.status == 200)
{
return System.Text.Encoding.Default.GetString((byte[])xml.responseBody);
}
else
{
return xml.status.ToString();
}
}
catch (Exception E)
{
return E.Message.ToString();
}
}
else if (method == SendMethod.GET)
{
xml.open("GET", p.Url + "?" + CommonFunction.UrlEncoding(p.Parameters), false, p.Uid, p.Pwd);
xml.send(null);
return System.Text.Encoding.Default.GetString((byte[])xml.responseBody);
}
return null;
}
#endregion
#region 構造與釋構
public XMLHTTP()
{
xml = new XMLHTTPClass();
}
~XMLHTTP()
{
Dispose();
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
if (isDisposing)
{
if (xml != null)
{
xml = null;
}
}
_alreadyDispose = true;
}
#endregion
#region IDisposable 成員
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}