How to Use asp.net to post a message to other servers

Source: Internet
Author: User

Abstract: How to Use asp.net to post a message to other servers

Using System;
Using System. Web;
Using System. Net;
Using System. IO;
Using System. Text;
Namespace SendMessage
{
Public bool SendMsg (MsgInfo msg)
{
// Create request
Try
{
WebRequest req = WebRequest. Create ("http: // your_post_url ");
// Set the request parameter
Req. Method = "POST ";
Req. ContentType = "application/x-www-form-urlencoded ";
// Querystring '? Msg = xxx & type = 0'
String strQuery = "msg = ";
StrQuery + = HttpUtility. UrlEncode (msg );
StrQuery + = "& type = 0 ";
String dataSend = strQuery;
Req. ContentLength = dataSend. Length;
Byte [] buff = Encoding. UTF8.GetBytes (dataSend );
Stream reqStream = req. GetRequestStream ();
ReqStream. Write (buff, 0, buff. Length );
ReqStream. Close ();
WebResponse rep = req. GetResponse ();
Stream repStream = rep. GetResponseStream ();
Encoding enc = Encoding. GetEncoding ("UTF-8 ");
StreamReader sr = new StreamReader (repStream, enc );
Char [] read = new Char [256];
Sr. Read (read, 0,256 );
Return true;
}
Catch (NotSupportedException ns)
{
Return false;
}
}
}

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.