程序抓取网站数据HttpWebRequest

下面的代码实现了与以前XMLHTTP类似的功能。代码如下:

HttpSendData.aspx

<%@ Page language="c#"%> <%@ Import Namespace = "System"%> <%@ Import Namespace = "System.Collections"%> <%@ Import Namespace = "System.Web"%> <%@ Import Namespace = "System.Web.UI"%> <%@ Import Namespace = "System.Web.UI.WebControls"%> <%@ Import Namespace = "System.Net"%> <%@ Import Namespace = "System.IO"%> <%@ Import Namespace = "System.Text"%> <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <script runat="server"> void Button1_Click(object sender, System.EventArgs e) { string strTitle = TextBox1.Text; string strDesc = TextBox2.Text; Encoding encoding = Encoding.GetEncoding("GB2312"); string postData = "Title=" + strTitle; string strUrl = "http://xml.sz.luohuedu.net/HttpReceiveData.aspx"; postData += ("&Desc=" + strDesc); byte[] data = encoding.GetBytes(postData); // 准备请求... HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl); 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(); Response.Redirect("HttpSendData.aspx"); } </script> </head> <body> <form id="HTTPPost" method="post" runat="server"> 标题:<asp:TextBox id="TextBox1" runat="server"></asp:TextBox> <br /> 内容: <br /> <asp:TextBox id="TextBox2" runat="server" TextMode="MultiLine" Rows="10" Columns="100"></asp:TextBox> <br /> <asp:Button id="Button1" runat="server" Text=" 发 送 " onclick="Button1_Click"></asp:Button> </form> </body> </html>

HttpReceiveData.aspx

<%@ Page Language="vb"%> <%@ import Namespace = "System" %> <%@ import Namespace = "System.Web.UI" %> <%@ import Namespace = "System.Web" %> <script runat="server"> Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) If Request.ServerVariables("REQUEST_METHOD").ToString() = "POST" Then Dim connstr As String connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Test.mdb") Dim cn As New System.Data.OleDb.OleDbConnection(connstr) Dim strSQL As String = "INSERT INTO TestTable (Title,Description) VALUES('" _ + Request.Form("Title").ToString() + "','" + Request.Form("Desc").ToString() + "')" cn.Open() Dim cmd As New System.Data.OleDb.OleDbCommand(strSQL, cn) cmd.ExecuteNonQuery() cn.Close() cn.Dispose() cmd.Dispose() End If End Sub </script>

补充code:


private   HttpWebRequest   CreateRequest()  
  {  
   
  HttpWebRequest   请求=null;  
  // string   aa=TxNetId();    
           
  请求=(HttpWebRequest)HttpWebRequest.Create(this._strUrl);//创建请求  
  请求.Accept   =   "*/*";   //接受任意文件  
  请求.UserAgent   =   "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.2;   .NET   CLR   1.1.4322)";   //   模拟使用IE在浏览  
  请求.AllowAutoRedirect   =   true;//这里不允许302  
  请求.CookieContainer   =comm.cookie;//cookie容器,  
  请求.Referer="http://bbs.tiexue.net";   //当前页面的引用  
     
   
  //如果附带cookie   就发送  
  if   (   this._cookiePost   !=null)  
  {  
  System.Uri   u   =new   Uri(_strUrl);    
                  //doenet处理cookie的bug:请求的服务器和cookie的Host必须一直,否则不发送或获取!  
   
  //这里修改成一致!  
  foreach(System.Net.Cookie   c   in   _cookiePost)  
  {  
  c.Domain=   u.Host;  
  }  
   
  请求.CookieContainer.Add(_cookiePost);  
  }  
   
  //如果需要发送数据,就以Post方式发送  
  if   (_strPostdata   !=null   &&   _strPostdata.Length>0)  
  {  
  //                                 请求.CookieContainer.Add(new   Cookie("TxNet","","/","bbs.tiexue.net"));  
  请求.ContentType   =   "application/x-www-form-urlencoded";//作为表单请求  
  请求.Method   =   "POST";//方式就是Post  
   
  //发送http数据:朝请求流中写post的数据  
  byte[]   b=   this._encoding.GetBytes(this._strPostdata);  
  请求.ContentLength=b.Length;  
  System.IO.Stream   sw=null;  
  try  
  {  
  sw=请求.GetRequestStream();  
  sw.Write(b,0,b.Length   );  
  }  
  catch   (System.Exception   ex)  
  {  
  this._strErr=ex.Message;  
  }  
  finally  
  {  
  if   (sw!=null){sw.Close();}  
  }  
   
  }  
  return   请求;   //返回创建的请求对象  
   
  }  
   
  public   string   Proc()      
  {  
                           
  HttpWebRequest   请求=CreateRequest();//请求  
  HttpWebResponse   响应=null;;  
     
  System.IO.StreamReader   sr=null;  
     
     
  try  
  {  
  //这里得到响应  
           
  响应   =(HttpWebResponse)请求.GetResponse();  
                             
  sr=new   System.IO.StreamReader(响应.GetResponseStream(),this.encoding);  
     
  this._ResHtml=sr.ReadToEnd();   //   这里假定响应的都是html文本  
  }  
  catch   (System.Exception   ex)  
  {  
  this._strErr=ex.Message;//发生错误就返回空文本  
  return   "";  
  }  
  finally  
  {  
  if   (sr!=null){sr.Close();}  
  }  
  //状态码  
  this._strCode=响应.StatusCode.ToString();  
   
    if   (this._strCode=="302")   //如果是302重定向的话就返回新的地址。  
  {  
  this._ResHtml=响应.Headers["location"];  
  }  
   
  //得到cookie  
  if(   响应.Cookies.Count   >   0)  
  {  
  this._cookieGet   =响应.Cookies;   //得到新的cookie:注意这里没考虑cookie合并的情况  
  }  
  return   this.ResHtml   ;  
  }

你可能感兴趣的:(request)