Chinaunix首页 | 论坛 | 博客
  • 博客访问: 219609
  • 博文数量: 53
  • 博客积分: 2626
  • 博客等级: 少校
  • 技术积分: 509
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-27 22:40
文章分类

全部博文(53)

文章存档

2012年(2)

2011年(13)

2010年(10)

2009年(28)

我的朋友

分类: 系统运维

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,@"","",RegexOptions.IgnoreCase);
   Htmlstring = Regex.Replace(Htmlstring,@"","",RegexOptions.IgnoreCase);
   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,@"
阅读(4398) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~