首页 > 编程 > .NET > 正文

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

2024-07-10 12:55:53
字体:
来源:转载
供稿:网友
摘要:如何用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;
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表