论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp.net教程
Tag:静态页面,treeview,gridview,repeater,dataset,sqldatareader,ado.net,上传,三层,ajax,xml,留言本,新闻发布,商城,注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,触发器,socket,form认证,登录,视频教程

用post方法从网上抓取信息

文章类别:Asp.net | 发表日期:2008-10-5 22:07:30

前些天有个要求要从某个网站上取信息,但是该网站要求用post方式访问的.现在这里贴两个google到的方法.

using System.Net;

static string WebClinetPost(string url, string postData,string encodeType,out string err)
  {
   string uriString = url; 
   byte[] byteArray; 
   byte[] responseArray; 
   //postData = "checkvalue=32&bbb=%CD%B6%C6%B1&ilc=0&kkk=22";
   Encoding encoding = Encoding.GetEncoding(encodeType);
   try
   {
    WebClient myWebClient = new WebClient();
    WebHeaderCollection myWebHeaderCollection; 
    myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); 
    //myWebClient.Headers.Add("Referer","http://xxxxx/xxxxxxxxxxxxxxxxxxxxxxxx"); 
    //myWebClient.Headers.Add("Accept-Language","zh-cn"); 
    myWebHeaderCollection = myWebClient.Headers; 
 
    byteArray = encoding.GetBytes(postData);
    responseArray = myWebClient.UploadData(uriString,"POST",byteArray); 

    err=string.Empty;
    return encoding.GetString(responseArray);
   }
   catch(Exception ex)
   {
    err=ex.Message;
    return string.Empty;
   }
  }
  public static string HttpWebResponsePost(string url, string postData,string encodeType,out string err)
  {

   Stream outstream = null;

   Stream instream = null;

   StreamReader sr = null;

   HttpWebResponse response = null;

   HttpWebRequest request = null;

   Encoding encoding = Encoding.GetEncoding(encodeType);

   byte[] data = encoding.GetBytes(postData);

   try

   {   

    request = WebRequest.Create(url) as HttpWebRequest;

    CookieContainer cookieContainer = new CookieContainer();

    request.CookieContainer = cookieContainer;

    request.AllowAutoRedirect = true;

    request.Method = "POST";

    request.ContentType = "application/x-www-form-urlencoded";

    request.ContentLength = data.Length;

    outstream = request.GetRequestStream();

    outstream.Write(data,0,data.Length);

    outstream.Close();

    response = request.GetResponse() as HttpWebResponse;
    instream = response.GetResponseStream();
    sr = new StreamReader( instream, encoding );
    string content = sr.ReadToEnd();

    err = string.Empty;
    return content;

   }

   catch(Exception ex)
   {

    err = ex.Message;
    return string.Empty;

   }
  }

调用代码:
   string url="http://xxxxxx/xxxxxxxx/scrrm00542.jsp";
   string postData="proj_id=600197&theSubmit=600197";
   string encodeType="utf-8";
   string err="";
   //string content=HttpWebResponsePost(url,postData,encodeType,out err);
   string content=WebClinetPost(url,postData,encodeType,out err);

另外,再附上javascript实现的代码吧:
<script language=javascript>
 function GetData()
 {

  var postData = "proj_id=600197&theSubmit=600197";
  var http = new GetXMLHTTPRequest();
  http.open('POST', 'http://xxxxxxxxxx/xxxxxx/scrrm00542.jsp', false);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http.send(postData);
 
  return http.responseText;
 }

 function GetXMLHTTPRequest()
 {
  var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
  for (var i = 0; i < progIDs.length; ++i)
  {
   var progID = progIDs[i];
   try
   {
    var x = new ActiveXObject(progID);
    return x;
   }
   catch (e)
   {}
  }
 }
</script>

http://www.cnblogs.com/lwyaster/archive/2007/01/09/615815.html

视频教程列表
文章教程搜索
 
Asp.net推荐教程
Asp.net热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058