zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Asp.netXMLHTTP封装类(GET,Post发送和接收数据)

ASP封装 发送 get post 接收数据
2023-06-13 09:14:06 时间
复制代码代码如下:

/****************************************************************
*函数名称:SendCommand(SendMethodmethod,ST_Paramp)
*功能说明:向远程发送URL和参数,接受返回信息(无乱码);
*参数:method:xml发送方法,POST/Get两种
P:参数结构体
publicstringUrl;//远程URL
publicstringParameters;//参数
publicstringUid;//帐号
publicstringPwd;//号令
*调用示列:
*usingebcnc;//引用空间名
*XMLHTTPx=newXMLHTTP();//创建设xmlhttp对像
*XMLHTTP.ST_Paramst=newXMLHTTP.ST_Param();//创建参数数组
*st.Parameters="";//url详细参数
*st.Url="http://www.baidu.com/";//url
*st.Uid="";//帐号
*st.Pwd="";//口令
*stringrn=””;//返回字符串
*rn=x.SendCommand(XMLHTTP.SendMethod.POST,st);//获取返回信息
*x.Dispose();

***********************************************************************/
usingSystem;
usingMSXML2;
namespaceebcnc
{
///<summary>
///XMLHTTP基类
///</summary>
publicclassXMLHTTP:IDisposable
{
#region变量及参数
privateXMLHTTPClassxml;
privatebool_alreadyDispose=false;

publicST_ParamParameters;

publicenumSendMethod:int
{
POST,GET
}
#endregion

#region参数结构体
publicstructST_Param
{
publicstringUrl;
publicstringParameters;
publicstringUid;
publicstringPwd;
}
#endregion

#region发送数据
///<summary>
///发送数据
///</summary>
///<paramname="method">发送方式</param>
///<paramname="p">数据</param>
///<returns>STRING</returns>
publicvirtualstringSendCommand(SendMethodmethod,ST_Paramp)
{
if(p.Url==null||p.Url=="")returnnull;
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)
{
returnSystem.Text.Encoding.Default.GetString((byte[])xml.responseBody);
}
else
{
returnxml.status.ToString();
}
}
catch(ExceptionE)
{
returnE.Message.ToString();
}
}
elseif(method==SendMethod.GET)
{
xml.open("GET",p.Url+"?"+CommonFunction.UrlEncoding(p.Parameters),false,p.Uid,p.Pwd);
xml.send(null);
returnSystem.Text.Encoding.Default.GetString((byte[])xml.responseBody);
}
returnnull;
}
#endregion

#region构造与释构
publicXMLHTTP()
{
xml=newXMLHTTPClass();
}
~XMLHTTP()
{
Dispose();
}
protectedvirtualvoidDispose(boolisDisposing)
{
if(_alreadyDispose)return;
if(isDisposing)
{
if(xml!=null)
{
xml=null;
}
}
_alreadyDispose=true;
}
#endregion

#regionIDisposable成员

publicvoidDispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

#endregion
}
}