php 和C# 获取网页header信息并获取转跳真实url的方法

2015年07月02日 11:32 by:唠吧小站

获取网页header信息并获取转跳真实url的方法,用获取header的方式来获取真实的路径:

php curl 获取header信息从而获得真实路径:

  1. <?php
  2. error_reporting(E_ALL | E_STRICT); 
  3. ini_set("display_errors", "On"); 
  4. header("Content-Type:text/html;charset=utf-8");
  5. date_default_timezone_set('PRC');
  6. $url = "www.fansart.com";
  7. $conn = curl_init($url);
  8. curl_setopt($conn, CURLOPT_TIMEOUT, 500); 
  9. curl_setopt($conn, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");   
  10. curl_setopt($conn, CURLOPT_ENCODING, 'gzip,deflate');
  11. curl_setopt($conn, CURLOPT_HEADER ,1);   
  12. curl_setopt($conn, CURLOPT_CONNECTTIMEOUT,60);  
  13. curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
  14. curl_setopt($conn, CURLOPT_NOBODY,1);//不用返回body部分内容
  15. curl_setopt($conn, CURLOPT_FOLLOWLOCATION,1); //抓取转跳
  16. $data = curl_exec($conn);
  17. curl_close($conn); 
  18. echo "<pre>";
  19. print_r($data);
  20. ?>

C#获取header转跳地址的代码如下:

  1. string bdurl;
  2. string realurl;
  3. request = (HttpWebRequest)WebRequest.Create(bdurl);
  4. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; MyIE2; .NET CLR 1.1.4322)";
  5. request.Method = "HEAD";
  6. request.AllowAutoRedirect = false;
  7. response = (HttpWebResponse)request.GetResponse();
  8. realurl =response.GetResponseHeader("Location");
标签:php c# header