跨服务器提交一条信息(POST方式)
发送参数形式:
string MailData = "from="+ MailFrom +"&to="+ MailTo + "&ck="+ MailCheckNumber +"&subject="+ MailSubject +"&content="+ MailContent;
写成一个POST类:
public class PostData
{
/// <summary>
/// 向sina服务器post信息
/// </summary>
/// <param name="SendUrl">目标接口地址</param>
/// <param name="Msg">发送参数</param>
/// <returns></returns>
public string SendMsg(string SendUrl,string Msg)
{
try
{
Encoding encoding = Encoding.GetEncoding("GB2312");
byte[] data = encoding.GetBytes(Msg);
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(SendUrl);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// 发送数据
newStream.Write(data, 0, data.Length);
newStream.Close();
//获取响应
string scode = "";
WebResponse response = myRequest.GetResponse();
using (System.IO.Stream resStream = response.GetResponseStream())
{
if (resStream != null)
{
using (StreamReader sr = new StreamReader(resStream))
{
scode = sr.ReadToEnd();
sr.Close();
}
}
resStream.Close();
}
return scode;
}
catch (NotSupportedException)
{
return "false";
}