A simple C # XMLHTTP call

Source: Internet
Author: User

Http://www.cnblogs.com/devsdk/archive/2009/04/11/1433475.html

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. net;

Namespace devsdk. Partners. dns_com_cn.sms
{
/// <Summary>
/// Webhttprequest basic class
/// </Summary>
Public class dswebhttprequest
{
/// <Summary>
/// Webhttprequest basic class
/// </Summary>
/// <Param name = "webmethod"> submission method post, get </param>
/// <Param name = "Surl"> submission path </param>
/// <Param name = "webpostdata"> parameter string </param>
/// <Returns> Returns HTML code </returns>
Internal string webhttprequest (string webmethod, string Surl, string webpostdata)
{
Byte [] sdata = encoding. Default. getbytes (webpostdata );

String fstring = "";

# Region "Web instance initialization"
Httpwebrequest webrequest = NULL;

Httpwebresponse webresponse = NULL;

Uri weburl = new uri (Surl );

Stream webstream = NULL;

Streamreader webreader = NULL;
# Endregion

Try
{
Webrequest = (httpwebrequest) webrequest. Create (weburl );

Webrequest. method = webmethod;

Webrequest. contenttype = "application/X-WWW-form-urlencoded ";

Webrequest. contentlength = sdata. length;

Try
{
Webstream = webrequest. getrequeststream ();

Webstream. Write (sdata, 0, sdata. Length );

Webstream. Close ();
}
Catch (ioexception ioex)
{
Throw ioex;
}
Finally
{
Webstream. Close (); webstream. Dispose (); // clear the instance
}

Webresponse = (httpwebresponse) webrequest. getresponse ();

Try
{
Webreader = new streamreader (webresponse. getresponsestream (), encoding. Default );

Fstring = webreader. readtoend ();

Return fstring;
}
Catch (ioexception ioex)
{
Throw ioex;
}
Finally
{
Webreader. Dispose ();
}
}
Catch (httplistenerexception httpex)
{
Throw httpex;
}
Finally
{
# Region "clear instance objects"
Webstream. Dispose ();
Webreader. Dispose ();
# Endregion
}
}

/// <Summary>
/// Security string Filtering
/// </Summary>
/// <Param name = "fstring"> unsafe string </param>
/// <Returns> Security string </returns>
Internal string htmlsqlencodes (string fstring)
{
String tempstr = "";
For (INT I = 0; I <fstring. length; I ++)
{
If (char. isnumber (fstring, I ))
{
Tempstr + = fstring. substring (I, 1 );
}
}
Return tempstr;
}

/// <Summary>
/// 32-bit MD5 Encryption
/// </Summary>
/// <Param name = "sstring"> string to be encrypted </param>
/// <Returns> encrypted string </returns>
Internal string MD5 (string sstring)
{
Byte [] DATA = system. Text. utf8encoding. utf8.getbytes (sstring );

System. Security. cryptography. MD5 MD5 = new system. Security. cryptography. md5cryptoserviceprovider ();

Byte [] result = md5.computehash (data );

Return bitconverter. tostring (result). Replace ("-","");
}
}
}

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.