程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 如何用asp.net向其他服務器post一條信息

如何用asp.net向其他服務器post一條信息

編輯:關於C#
 

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;
}
}

}
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved