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 ("-","");
}
}
}