嗨,我正在使用以下代码通过HttpClient向webpage发送请求:
Imports System.Net
Imports System.Net.Http
Public Class Form1
Dim client As HttpClient = New HttpClient()
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Set User-Agent header
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
' Send request and receive response
Dim response As HttpResponseMessage = client.GetAsync("https://ikalogs.ru/tools/map/?page=1&server=22&world=10&state=active&search=city&allies%5B1%5D=&allies%5B2%5D=&allies%5B3%5D=&allies%5B4%5D=&nick=Bauer&ally=&island=&city=&x=&y=").Result
If response.IsSuccessStatusCode Then
' Get response content as string
Dim vcmode As String = response.Content.ReadAsStringAsync().Result
' Print response content
RichTextBox1.Text = vcmode
End If
End Sub
End Class
我使用的是最新的用户代理我从https://www.useragentstring.com/,但回应一直说浏览器过时:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Browser is out of date!</title>
<meta charset="utf-8">
<meta name="description" content="Ikalogs - Saving battle reports" />
<meta name="author" content="ZigFreeD" />
<meta name="keywords" content="ikalogs, логовица, анализатор, ikariam" />
<meta name="copyright" content="(c) 2012-2015 by ZigFreeD"/>
<meta http-equiv="pragma" content="no-cache"/>
<meta name="language" content=""/>
<base target=”_blank” />
<noscript>
<meta http-equiv="refresh" content="0; url=/default/jsdisabled/">
</noscript>
<style>
*{
margin: 0;
padding: 0;
color: #542c0f;
font: 700 20px/27px Arial,sans-serif;
text-align: center;
text-decoration: none;
}
body{
background: url(/themes/default/img/themes/error/background.jpg) #eedbb2;
}
.errorBrowser{
width: 500px;
min-height: 190px;
position: fixed;
left: 50%;
top: 50%;
margin: -95px 0 0 -250px;
}
.errorIcoDesk{
font: italic 700 14px/18px Arial,sans-serif;
background: url(/themes/default/img/themes/browsers.png) no-repeat top left;
width: 50px;
float: left;
padding: 100px 25px 0;
cursor: pointer;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=80);
opacity: .8;
}
.errorIcoDesk:hover{
filter: progid:DXImageTransform.Microsoft.Alpha(opacity=100);
opacity: 1;
}
.errorFi{background-position: -100px 0;}
.errorCh{background-position: -200px 0;}
.errorOp{background-position: -300px 0;}
.errorEx{background-position: -400px 0;}
</style>
</head>
<body>
<div class="errorBG">
<div class="errorBrowser">
<h1>Your browser is outdated or does not support the necessary technology for the operation of the site.</h1>
<a href="//www.apple.com/safari/">
<div class="errorIcoDesk errorSa">
Apple Safari
</div>
</a>
<a href="//www.mozilla.com/firefox/">
<div class="errorIcoDesk errorFi">
Mozilla Firefox
</div>
</a>
<a href="//www.google.com/chrome/">
<div class="errorIcoDesk errorCh">
Google Chrome
</div>
</a>
<a href="//www.opera.com/">
<div class="errorIcoDesk errorOp">
Opera
</div>
</a>
<a href="//ie.microsoft.com/">
<div class="errorIcoDesk errorEx">
Internet Explorer
</div>
</a>
</div>
</div>
</body>
</html>
我一直在尝试不同的用户代理,但这是最新的,我可以找到在线和它似乎很奇怪,该页不接受这一个.我想我做错了什么,而添加用户代理到标题.
1条答案
按热度按时间3xiyfsfu1#
非常重要:切勿在此平台中使用
.Result
或.Wait()
您通常需要对HttpClient进行更多的配置。
最好总是添加一个CookieContainer并指定使用Cookie(尽管这是CookieContainer存在时的默认行为)。
然后最好添加头文件,指定支持解压缩和处理什么解压缩方法,否则你可能会得到 * 垃圾 *(不是在这个特定的情况下,但是,好吧,因为你在那里...)
现在,用户代理可以是几乎任何可以识别的东西
在这里,我使用Lazy initialization作为
Lazy<T>(Func<T>)
。我发现它很有用,因为它允许内联HttpClient对象的配置,还可以添加一个带有Lambda的已配置HttpClientHandler,还因为您可以创建包含HttpClient的类,但实际上从未使用过它(该对象在您请求
Lazy<T>.Value
时初始化)之后,HttpClient的请求几乎可以充当WebBrowser(除非Web站点挑战您的请求并等待响应-HSTS和其他不同的技巧-否则您什么也做不了)
请注意,您帖子中的网站并不安全