分类: 系统运维
2009-08-20 00:58:45
1、System.Net.HttpWebRequest 类
如下代码以Post方式发送数据:
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
private void btnConfirm_Click(object sender, System.EventArgs e)
{
string UserName = txtUserName.Text;
string SpotID = txtSpotID.Text;
string BBS = txtBBS.Text;
string Board = txtBoard.Text;
string Title = txtTitle.Text;
string Content = txtContent.Text;
Content = NoHTML(Content);//去除HTML标记
string UserIp = Request.ServerVariables["REMOTE_HOST"].ToString();
string PostUrl = "";
string PostDateTime = DateTime.Now.ToString();
string WebSite = "";//服务器接收页面
WebSite = System.Configuration.ConfigurationSettings.AppSettings["PostUrl"];
string strQuery = "UserName="+ UserName +"&SpotID="+ SpotID +"&BBS="+ BBS +"&Board="+ Board +
"&Title="+ Title +"&Content="+ Content +"&UserIp="+ UserIp +"&PostUrl="+ PostUrl +"&PostDateTime="+ PostDateTime;//数据参数队列
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(strQuery);
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(WebSite);
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();
Page.RegisterStartupScript("script",GetShowAlert("发表成功!","window.location.href('ClientPostNote.aspx');"));
}
///
/// 去除HTML标记
///
/// 包括HTML的源码
///
public string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Htmlstring.Replace("\r\n","");
Htmlstring = Regex.Replace(Htmlstring,@"
Htmlstring = Regex.Replace(Htmlstring,@"
Htmlstring = Regex.Replace(Htmlstring,@"<.*?>","",RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring,@"<(.[^>]*)>","",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"([\r\n])[\s]+","",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"-->","",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"