用post方法從網上抓取資訊

來源:互聯網
上載者:User

前些天有個要求要從某個網站上取資訊,但是該網站要求用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",""); 
    //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="";
   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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.