asp.net XMLHTTP encapsulation class (Get,post send and receive data) _ Practical Tips

Source: Internet
Author: User
Copy Code code as follows:

/****************************************************************
* Function Name: SendCommand (Sendmethod method, St_param p)
* Function Description: Send the URL and parameters to the remote, accept the return information (no garbled);
* Parameter: Method:xml send method, post/get two kinds
P: Parametric structural body
public string Url; Remote URL
public string Parameters; Parameters
public string Uid; Account
public string Pwd; Command
* Call Columns:
* Using EBCNC; Reference Space Name
* XMLHTTP x = new XMLHTTP (); The XMLHTTP of creating construction
* XMLHTTP. St_param st = new XMLHTTP. St_param (); Creating an array of parameters
* St. Parameters = ""; URL Detail Parameters
* St. URL = "http://www.baidu.com/"; Url
* St. Uid = ""; Account
* St. PWD = ""; Password
* String rn= ""; return string
* Rn=x.sendcommand (XMLHTTP. Sendmethod.post, ST); Get return information
* X.dispose ();

***********************************************************************/
Using System;
Using MSXML2;
Namespace EBCNC
{
<summary>
XMLHTTP base class
</summary>
public class Xmlhttp:idisposable
{
#region Variables and parameters
Private Xmlhttpclass XML;
private bool _alreadydispose = false;

Public St_param Parameters;

public enum Sendmethod:int
{
POST, get
}
#endregion

#region parameter Structural body
public struct St_param
{
public string Url;
public string Parameters;
public string Uid;
public string Pwd;
}
#endregion

#region Send data
<summary>
Send data
</summary>
<param name= "Method" > Delivery </param>
<param name= "P" > Data </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 structure and its interpretation
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 Members

public void Dispose ()
{
Dispose (TRUE);
Gc. SuppressFinalize (this);
}

#endregion
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.