最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 關(guān)于爬取網(wǎng)站數(shù)據(jù)遇到csrf-token的分析與解決

 更新時(shí)間:2021年01月28日 08:45:51   作者:yibey  
這篇文章主要介紹了C# 關(guān)于爬取網(wǎng)站數(shù)據(jù)遇到csrf-token的分析與解決,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下

需求

某航空公司物流單信息查詢,是一個(gè)post請(qǐng)求。通過后臺(tái)模擬POST HTTP請(qǐng)求發(fā)現(xiàn)無法獲取頁面數(shù)據(jù),通過查看航空公司網(wǎng)站后,發(fā)現(xiàn)網(wǎng)站使用避免CSRF攻擊機(jī)制,直接發(fā)揮40X錯(cuò)誤。

關(guān)于CSRF

讀者自行百度

網(wǎng)站HTTP請(qǐng)求分析 

Headers 

Form Data

在head里包含了cookie 與 x-csrf-token  formdata 里包含了_csrf (與head里的值是一樣的).

這里通過查看該網(wǎng)站的JS源代碼發(fā)現(xiàn)_csrf 來自于網(wǎng)頁的head標(biāo)簽里

猜測(cè)cookie與 x-csrf-token是有一定的有效期,并且他們共同作用來防御CSRF攻擊。

解決方案

1,首先請(qǐng)求一下該航空公司的網(wǎng)站,獲取cookie與_csrf

2,然后C# 模擬http分別在head和formdata里加入如上參數(shù),發(fā)起請(qǐng)求

代碼

public class CSRFToken
 {
 string cookie;//用于請(qǐng)求的站點(diǎn)的cookie
 List<string> csrfs;//用于請(qǐng)求站點(diǎn)的token的key 以及 value

 public CSRFToken(string url)
 {
  //校驗(yàn)傳輸安全
  if (!string.IsNullOrWhiteSpace(url))
  {
  try
  {
   //設(shè)置請(qǐng)求的頭信息.獲取url的host
   var _http = new HttpHelper(url);
   string cookie;
   string html = _http.CreateGetHttpResponseForPC(out cookie);
   this.cookie = cookie;

   string headRegex = @"<meta name=""_csrf.*"" content="".*""/>";

   MatchCollection matches = Regex.Matches(html, headRegex);
   Regex re = new Regex("(?<=content=\").*?(?=\")", RegexOptions.None);
   csrfs = new List<string>();
   foreach (Match math in matches)
   {

   MatchCollection mc = re.Matches(math.Value);
   foreach (Match ma in mc)
   {
    csrfs.Add(ma.Value);
   }
   }

  }
  catch (Exception e)
  {

  }
  }
 }

 public String getCookie()
 {
  return cookie;
 }
 public void setCookie(String cookie)
 {
  this.cookie = cookie;
 }
 public List<string> getCsrf_token()
 {
  return csrfs;
 }
 }

httpHelper

public string CreatePostHttpResponse(IDictionary<string, string> headers, IDictionary<string, string> parameters)
  {
   HttpWebRequest request = null;
   //HTTPSQ請(qǐng)求 
   UTF8Encoding encoding = new System.Text.UTF8Encoding();
   ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
   request = WebRequest.Create(_baseIPAddress) as HttpWebRequest;
   request.ProtocolVersion = HttpVersion.Version10;
   ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
   request.Method = "POST";
   request.ContentType = "application/x-www-form-urlencoded";
   // request.ContentType = "application/json";
   request.UserAgent = DefaultUserAgent;
   //request.Headers.Add("X-CSRF-TOKEN", "bc0cc533-60cc-484a-952d-0b4c1a95672c");
   //request.Referer = "https://www.asianacargo.com/tracking/viewTraceAirWaybill.do";

   //request.Headers.Add("Origin", "https://www.asianacargo.com");
   //request.Headers.Add("Cookie", "JSESSIONID=HP21d2Dq5FoSlG4Fyw4slWwHb0-Sl1CG6jGtj7HE41e5f4aN_R1p!-435435446!117330181");
   //request.Host = "www.asianacargo.com";


   if (!(headers == null || headers.Count == 0))
   {

    foreach (string key in headers.Keys)
    {
     request.Headers.Add(key, headers[key]);
    }

   }


   //如果需要POST數(shù)據(jù)  
   if (!(parameters == null || parameters.Count == 0))
   {
    StringBuilder buffer = new StringBuilder();
    int i = 0;
    foreach (string key in parameters.Keys)
    {
     if (i > 0)
     {
      buffer.AppendFormat("&{0}={1}", key, parameters[key]);
     }
     else
     {
      buffer.AppendFormat("{0}={1}", key, parameters[key]);
     }
     i++;
    }
    byte[] data = encoding.GetBytes(buffer.ToString());
    using (Stream stream = request.GetRequestStream())
    {
     stream.Write(data, 0, data.Length);
    }
   }

   HttpWebResponse response;

   try
   {
    //獲得響應(yīng)流
    response = (HttpWebResponse)request.GetResponse();
    Stream s = response.GetResponseStream();

    StreamReader readStream = new StreamReader(s, Encoding.UTF8);
    string SourceCode = readStream.ReadToEnd();
    response.Close();
    readStream.Close();
    return SourceCode;
   }
   catch (WebException ex)
   {
    response = ex.Response as HttpWebResponse; return null;
   }

  }

 public string CreateGetHttpResponse(out string cookie)
  {
   HttpWebRequest request = null;
   //HTTPSQ請(qǐng)求 
   UTF8Encoding encoding = new System.Text.UTF8Encoding();
   ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
   request = WebRequest.Create(_baseIPAddress) as HttpWebRequest;
   request.ProtocolVersion = HttpVersion.Version10;
   ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
   request.Method = "GET";
   request.ContentType = "application/x-www-form-urlencoded";
   request.UserAgent = DefaultUserAgent;

   HttpWebResponse response;

   try
   {
    //獲得響應(yīng)流
    response = (HttpWebResponse)request.GetResponse();

    cookie = response.Headers["Set-Cookie"];
    Stream s = response.GetResponseStream();

    StreamReader readStream = new StreamReader(s, Encoding.UTF8);
    string SourceCode = readStream.ReadToEnd();
    response.Close();
    readStream.Close();
    return SourceCode;
   }
   catch (WebException ex)
   {
    response = ex.Response as HttpWebResponse;
    cookie = "";
    return null;
   }

  }

爬取程序

爬取結(jié)果

瀏覽器結(jié)果

注意事項(xiàng)與結(jié)論

1,不同的網(wǎng)站,獲取cstf的方式不一樣,無論怎么做,只要信息傳到前臺(tái)我們都可以有相應(yīng)的方法來獲取。

2,請(qǐng)求時(shí)候的http驗(yàn)證可能不一樣,測(cè)試的某航空公司物流信息的時(shí)候,http請(qǐng)求的安全協(xié)議是tis12。

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; 還有其他參數(shù)比如UserAgent后臺(tái)可能也會(huì)驗(yàn)證

3,基于如上航空公司,發(fā)現(xiàn)它的cookie和cstf_token一定時(shí)間內(nèi)不會(huì)改變,那么當(dāng)實(shí)際爬取的時(shí)候可以考慮緩存cookie以及cstf_token,只有當(dāng)請(qǐng)求失敗的時(shí)候,才重新獲取

相關(guān)文章

  • C# 枚舉的使用簡介

    C# 枚舉的使用簡介

    這篇文章主要介紹了C# 枚舉的簡單使用,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • WinForm實(shí)現(xiàn)按名稱遞歸查找控件的方法

    WinForm實(shí)現(xiàn)按名稱遞歸查找控件的方法

    這篇文章主要介紹了WinForm實(shí)現(xiàn)按名稱遞歸查找控件的方法,需要的朋友可以參考下
    2014-08-08
  • C#中的Linq?To?XML講解

    C#中的Linq?To?XML講解

    本文詳細(xì)講解了C#中的Linq?To?XML,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • c#系列 list詳情

    c#系列 list詳情

    這篇文章主要介紹了c#系列 list,list 本質(zhì)是一個(gè)數(shù)組,。就跟我們操作系統(tǒng)一樣,提前申請(qǐng)內(nèi)存大小。所以我們程序一般都有一個(gè)申請(qǐng)內(nèi)存,實(shí)際使用內(nèi)存,內(nèi)存碎片這幾個(gè)概念,下面?zhèn)z看文章詳細(xì)內(nèi)容吧
    2021-10-10
  • C#實(shí)現(xiàn)驗(yàn)證身份證是否合法的方法

    C#實(shí)現(xiàn)驗(yàn)證身份證是否合法的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)驗(yàn)證身份證是否合法的方法,實(shí)例分析了通過自定義函數(shù)實(shí)現(xiàn)針對(duì)身份證合法性驗(yàn)證的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • WPF實(shí)時(shí)繪制心率曲線的示例詳解

    WPF實(shí)時(shí)繪制心率曲線的示例詳解

    這篇文章主要為大家詳細(xì)介紹了WPF實(shí)時(shí)繪制心率曲線的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • 字符串轉(zhuǎn)換成枚舉類型的方法

    字符串轉(zhuǎn)換成枚舉類型的方法

    字符串可以向int, bool等類型轉(zhuǎn)變,但是字符串是否可以向枚舉轉(zhuǎn)變呢?一起看下邊的例子
    2014-01-01
  • C#數(shù)組應(yīng)用分析

    C#數(shù)組應(yīng)用分析

    C#數(shù)組應(yīng)用分析...
    2007-08-08
  • c# wpf如何使用Blend工具繪制Control樣式

    c# wpf如何使用Blend工具繪制Control樣式

    這篇文章主要介紹了c# wpf如何使用Blend工具繪制Control樣式,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#程序中類數(shù)量對(duì)程序啟動(dòng)的影響詳解

    C#程序中類數(shù)量對(duì)程序啟動(dòng)的影響詳解

    這篇文章主要給大家介紹了關(guān)于C#程序中類數(shù)量對(duì)程序啟動(dòng)的影響的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10

最新評(píng)論

宜春市| 清流县| 耿马| 宁波市| 政和县| 永济市| 叶城县| 宁陕县| 铁力市| 陇西县| 永宁县| 乌拉特前旗| 成安县| 汶上县| 榆树市| 沅江市| 游戏| 九江县| 平果县| 汝州市| 大庆市| 陆川县| 鲁甸县| 且末县| 北宁市| 望城县| 浦县| 哈尔滨市| 会昌县| 舞阳县| 镇巴县| 和硕县| 福海县| 旬阳县| 白朗县| 安宁市| 宁明县| 福海县| 萨迦县| 茂名市| 惠安县|