Visual Basic 如何从 html 字符串中获取 <meta name="csrf_token" content="
Visual Basic how to get <meta name="csrf_token" content=" from html string
我正在登录一个站点(在我的 LAN 中),对于密码哈希,我需要 csrf 登录令牌。
此代码从站点加载 HTML 并将数据粘贴到 richtextbox:
Dim tempcookies As New CookieContainer
Dim xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.open("GET", "http://" & ipaddressbox.Text & "/html/home.html", False)
xmlhttp.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0")
xmlhttp.send()
If (xmlhttp.readyState = 4) Then
Dim XMLResponse As String = xmlhttp.responseText
tempcookies = xmlhttp.getResponseHeader("Set-Cookie")
logincookie = tempcookies
RichTextBox1.Text = XMLResponse + xmlhttp.getAllResponseHeaders()
End If
结果如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="csrf_token" content="bVH41vpXbsswSSfNt3Ifw1ZClIKf83Zg"/>
<meta name="csrf_token" content="586yr+JH0dwsLFWZcndPQpx4wGKErie6"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="../css/main.css" />
<script type="text/javascript" src="../lib/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../lib/log4javascript_lite.js"></script>
<script type="text/javascript" src="../lib/jquery.qtip.js"></script>
<script type="text/javascript" src="../js/changelang.js"></script>
<script type="text/javascript" src="../js/main.js"></script>
<script type="text/javascript" src="../js/redirect.js"></script>
<script type="text/javascript" src="../js/validation.js"></script>
<script type="text/javascript" src="../js/home.js"></script>
<title></title>
</head>
<body>
<div id="all_content" class="body_bg">
<div id="login_wrapper" class="login_wrapper_dim">
.....etc, continues....
我需要从以下位置获取字符串:meta name="csrf_token" content="THESTRING" 并从第二个 csrf_token.
我怎样才能做到这一点?
谢谢!
由于您使用的是 HTML,因此请使用 HTML 解析器,例如 HTML Agility Pack。
然后获取您要查找的值就像
一样简单
Dim doc = new HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(yourHtmlAsString)
Dim result = doc.DocumentNode.SelectNodes("//meta[@name='csrf_token']") _
.Select(Function(node) node.Attributes("content").Value) _
.ToList()
result
现在是包含值 "bVH41vpXbsswSSfNt3Ifw1ZClIKf83Zg"
和 "586yr+JH0dwsLFWZcndPQpx4wGKErie6"
的 List(Of String)
我正在登录一个站点(在我的 LAN 中),对于密码哈希,我需要 csrf 登录令牌。
此代码从站点加载 HTML 并将数据粘贴到 richtextbox:
Dim tempcookies As New CookieContainer
Dim xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.open("GET", "http://" & ipaddressbox.Text & "/html/home.html", False)
xmlhttp.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0")
xmlhttp.send()
If (xmlhttp.readyState = 4) Then
Dim XMLResponse As String = xmlhttp.responseText
tempcookies = xmlhttp.getResponseHeader("Set-Cookie")
logincookie = tempcookies
RichTextBox1.Text = XMLResponse + xmlhttp.getAllResponseHeaders()
End If
结果如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="csrf_token" content="bVH41vpXbsswSSfNt3Ifw1ZClIKf83Zg"/>
<meta name="csrf_token" content="586yr+JH0dwsLFWZcndPQpx4wGKErie6"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="../css/main.css" />
<script type="text/javascript" src="../lib/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../lib/log4javascript_lite.js"></script>
<script type="text/javascript" src="../lib/jquery.qtip.js"></script>
<script type="text/javascript" src="../js/changelang.js"></script>
<script type="text/javascript" src="../js/main.js"></script>
<script type="text/javascript" src="../js/redirect.js"></script>
<script type="text/javascript" src="../js/validation.js"></script>
<script type="text/javascript" src="../js/home.js"></script>
<title></title>
</head>
<body>
<div id="all_content" class="body_bg">
<div id="login_wrapper" class="login_wrapper_dim">
.....etc, continues....
我需要从以下位置获取字符串:meta name="csrf_token" content="THESTRING" 并从第二个 csrf_token.
我怎样才能做到这一点? 谢谢!
由于您使用的是 HTML,因此请使用 HTML 解析器,例如 HTML Agility Pack。
然后获取您要查找的值就像
一样简单Dim doc = new HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(yourHtmlAsString)
Dim result = doc.DocumentNode.SelectNodes("//meta[@name='csrf_token']") _
.Select(Function(node) node.Attributes("content").Value) _
.ToList()
result
现在是包含值 "bVH41vpXbsswSSfNt3Ifw1ZClIKf83Zg"
和 "586yr+JH0dwsLFWZcndPQpx4wGKErie6"
List(Of String)