首页 | 社区 | 博客 | 招聘 | 文章 | 新闻 | 下载 | 读书 | 代码
亲,您未登录哦! 登录 | 注册

如何用asp.net向其他服务器post一条信息

打印文章

分享到:
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;
}
}

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )

编程爱好者论坛

本栏最新文章