Const String Suseragent = " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;. Net CLR 1.1.4322;. Net CLR 2.0.50727) " ;
Const String Scontenttype = " Application/X-WWW-form-urlencoded " ;
Const String Srequestencoding = " UTF-8 " ;
Const String Sresponseencoding = " UTF-8 " ;
/// <Summary>
/// Post Data to URL
/// </Summary>
/// <Param name = "data"> Data to be post </Param>
/// <Param name = "url"> Target URL </Param>
/// <Returns> Server Response </Returns>
Public Static String Postdatatourl ( String Data, String URL)
{
Encoding = Encoding. getencoding (srequestencoding );
Byte [] Bytestopost = Encoding. getbytes (data );
Return Postdatatourl (bytestopost, URL );
}
/// <Summary>
/// Post Data to URL
/// </Summary>
/// <Param name = "data"> Data to be post </Param>
/// <Param name = "url"> Target URL </Param>
/// <Returns> Server Response </Returns>
Public Static String Postdatatourl ( Byte [] Data, String URL)
{
# Region Create an httpwebrequest object
Webrequest = Webrequest. Create (URL );
Httpwebrequest httprequest = Webrequest As Httpwebrequest;
If (Httprequest = Null )
{
Throw New Applicationexception (
String . Format ( " Invalid URL string: {0} " , URL)
);
}
# Endregion
# Region Fill in the basic information of httpwebrequest
Httprequest. useragent = Suseragent;
Httprequest. contenttype = Scontenttype;
Httprequest. Method = " Post " ;
# Endregion
# Region Fill in the content to post
Httprequest. contentlength = Data. length;
Stream requeststream = Httprequest. getrequeststream ();
Requeststream. Write (data, 0 , Data. Length );
Requeststream. Close ();
# Endregion
# RegionSend a POST request to the server and read the server's returned information
Stream responsestream;
Try
{
Responsestream=Httprequest. getresponse (). getresponsestream ();
}
Catch (Exception E)
{
// Log error winform debugging method
// Console. writeline (
// String. Format ("post operation exception: {0}", E. Message)
// );
Throw E;
}
# Endregion
# Region Read Server Response Information
String Stringresponse = String . Empty;
Using (Streamreader responsereader =
New Streamreader (responsestream, encoding. getencoding (sresponseencoding )))
{
Stringresponse = Responsereader. readtoend ();
}
Responsestream. Close ();
# Endregion
Return Stringresponse;
}
/// <Summary>
/// Encode the character as base64
/// </Summary>
/// <Param name = "encodetype"> Encoding Method </Param>
/// <Param name = "input"> Plaintext characters </Param>
/// <Returns> String </Returns>
Public Static String Encodebase64 ( String Encodetype, String Input)
{
String Result = String . Empty;
Byte [] Bytes = Encoding. getencoding (encodetype). getbytes (input );
Try
{
Result = Convert. tobase64string (bytes );
}
Catch
{
Result = Input;
}
Return Result;
}
/// <Summary>
/// Encode the character as base64
/// </Summary>
/// <Param name = "encodetype"> Encoding Method </Param>
/// <Param name = "input"> Plaintext characters </Param>
/// <Returns> String </Returns>
Public Static String Decodebase64 ( String Encodetype, String Input)
{
String Decode = String . Empty;
Byte [] Bytes = Convert. frombase64string (input );
Try
{
Decode = Encoding. getencoding (encodetype). getstring (bytes );
}
Catch
{
Decode = Input;
}
Return Decode;
}