程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 用post方法從網上抓取信息

用post方法從網上抓取信息

編輯:.NET實例教程

前些天有個要求要從某個網站上取信息,但是該網站要求用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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved