用post方法从网上抓取信息

2016-02-19 21:52 10 1 收藏

有一种朋友不在生活里,却在生命力;有一种陪伴不在身边,却在心间。图老师即在大家的生活中又在身边。这么贴心的服务你感受到了吗?话不多说下面就和大家分享用post方法从网上抓取信息吧。

【 tulaoshi.com - Web开发 】

  前些天有个要求要从某个网站上取信息,但是该网站要求用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)
    {

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

     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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

     {   

      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

来源:https://www.tulaoshi.com/n/20160219/1627336.html

延伸阅读
标签: Delphi
  在网络管理中,有时需要通过监视远程计算机屏幕来了解网上微机的使用情况。虽然,市面上有很多软件可以实现该功能,有些甚至可以进行远程控制,但在使用上缺乏灵活性,如无法指定远程计算机屏幕区域的大小和位置,进而无法在一屏上同时监视多个屏幕。其实,可以用Delphi自行编制一个灵活的远程屏幕抓取工具,简述如下。 一...
标签: ASP
  在实际的编程中,很多时候我们需要在多行的文本框中显示读取的信息,但是使用 一般的方法是不行的,比如 <%=rs("字段名")%,结果就会不能按者您的要求输出正 确的结果,在这个时候,我们就需要使用一些别的方法,下面是我在实际中使用的一 些方法,具体的代码请见: <textarea rows="4" name="remark&q...
Post-it系列创意广告作品 New-Skin Liquid Bandages系列创意广告1 New-Skin Liquid Bandages系列创意广告2 New-Skin Liquid Bandages系列创意广告3 New-Skin Liquid Bandages系列创意广告4 New-Skin Liquid Bandages系列创意广告5 La Fabbrica di Nichi系列创意广告1 La Fabbrica di Nichi系列创意广...
标签: 办公软件
WORD 2000提供了一种崭新的Web协作工作方式,通过它我们可在局域网及互联网上“开会”。 1. 安排会议 单击“工具”菜单之“联机协作”。注意:如果此时你的WORD中该菜单文字皆为灰色,那么,对不起,它表示你在安装OFFICE2000时没有装载可选项“MS NetMeeting(微软网络会议程序)”,而此程序却恰恰是本文的主角。请将它安装上。 ...
标签: Web开发
注意:$.get()和$.post()方法是jQuery中的全局函数。前面讲到的load()方式是对jQuery对象进行操作的。 1、 $.get()方法 $.get()方法使用GET方式来进行异步请求。 它的语法结构为: $.get( url [, data] [, callback] [, type] ) $.get()方法参数解释如下表: 参数名称 类 型 说 明 urlString请求的HTML页的URL...

经验教程

387

收藏

25
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部