我是使用HttpWebRequest
http://somedomain.tld
API编写的网络抓取工具。
我想从somedomain.tld
下载一些内容,但最终可能会发送大量请求。我不知道ServicePoint
是否会及时作出回应,我希望每次请求不超过10秒才能完成回复。我还想利用CancellationTokenSource
为该域强制执行的连接限制。
所以我需要能够超时请求。通常情况下,我会从var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10d))
获取取消令牌:
myWebRequest.Abort()
并将其提供给我的异步操作,也可能注册一个调用public async Task<byte[]> GetResponseData(Uri uri, CancellationToken ct)
{
var wr = (HttpWebRequest)WebRequest.Create(uri);
ct.Register(wr.Abort);
using(var response = await wr.GetResponseAsync())
using(var ms = new MemoryStream())
using(var responseStream = response.GetResponseStream())
{
await responseStream.CopyToAsync(ms,4096,ct);
return ms.ToArray();
}
}
的取消回调,所以我最终得到一个类似这样的(简化)方法:
var uri = new Uri("http://somedomain.tld");
var sp = ServicePointManager.FindServicePoint(uri);
sp.ConnectionLimit = 1;
到目前为止,非常好。
让我稍微约束一下:
ServicePoint
现在,与somedomain.tld
关联的ServicePoint
实例一次只允许一个请求。
现在我同时发出两个请求,安全地知道var dataTasks = Enumerable.Range(0,2).Select(async _=>{
using(var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10d)))
{
return await GetResponseData(uri,cts.Token);
}
});
var datas = await Task.WhenAll(dataTasks);
会将目标域与我的滥用隔离开来:
ServicePoint
现在,让我们假设第一个请求需要10多个才能完成...因为我已经限制ServicePoint
一次只触发一个请求,到ServicePoint
获取时为解雇第二个请求,它已被取消并中止。
那么我如何知道请求 实际 的发送时间?如何设置“了解” <?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'marc@**************.com';
$subject = 'New enquiry from website';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="emailSent">Thanks. Your message has been sent! We will be in touch shortly.</p>';
} else {
echo '<p class="errorMessage">Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p class="errorMessage">You answered the anti-spam question incorrectly. Please try again.</p>';
}
?>
<div class="large-6 medium-6 small-12 columns form-left">
<form class="footer-form" method="post" action="index.php">
<div class="row">
<div class="medium-9 medium-push-3 columns">
<label>
<input name="name" type="name" id="name" value="" placeholder="Your name" />
</label>
</div>
</div>
<div class="row">
<div class="medium-9 medium-push-3 columns">
<label>
<input name="email" type="email" id="email" value="" placeholder="Email Address" />
</label>
</div>
</div>
<div class="row">
<div class="medium-9 medium-push-3 columns">
<label>
<textarea name="message" id="message" placeholder="Your enquiry"></textarea>
</label>
</div>
</div>
<div class="row">
<div class="medium-9 medium-push-3 columns">
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" id="name" placeholder="Type Here">
</div>
</div>
<div class="row">
<div class="medium-9 medium-push-3 columns">
<button class="submit contact-button" id="name" name="submit" type="submit" value="Send email">Send</button>
</div>
</div>
</form>
</div>
针对特定请求的操作的超时?