ASP. Net Post Data

Source: Internet
Author: User
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;
}

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.