使用具有已知 IP 端点的 System.Net.WebRequest [英] Using System.Net.WebRequest with a known IP end point

查看:28
本文介绍了使用具有已知 IP 端点的 System.Net.WebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量已解析为 IP 地址的 DNS 名称.有了这个集合,我需要从他们那里下载 HTML.这是一个非常大的清单,我需要尽可能高效地完成.

I have a large collection of DNS names that have already been resolved to IP addresses. With this collection I need to download HTML from them. It's a very large list and I need to do it as efficiently as possible.

我正在使用 System.Net.HttpWebRequest 从每个域下载 HTML.HttpWebRequest 正在重复 DNS 查找,这会增加连接时间.我已经运行了一些测试,看看端口 80 上这些 IP 的套接字是否会连接得更快,而且它们确实如此.

I'm using System.Net.HttpWebRequest to download HTML from the each domain. HttpWebRequest is repeating the DNS lookup, and this is adding to the connection time. I've run tests to see if sockets for those IPs on port 80 would connect faster and they do.

所以我想使用具有已知 IP 地址的 HttpWebRequest,但我不知道如何使用.所有 WebRequest 工厂方法都需要一个 URL.

So I'd like to use HttpWebRequest with a known IP address, but I don't know how. All WebRequest factory methods require a URL.

现在我想我可以做这样的事情(其中 1.2.3.4 是 IP)

Now I thought I could do something like this (where 1.2.3.4 is the IP)

var req = WebRequest.Create("http://1.2.3.4/");
req.Headers.Add(....); <-- add something here

我需要以某种方式将目标域的内容添加到 HTTP 标头中,但我不知道该怎么做.

I need to somehow add to the HTTP header what the target domain is, but I'm not sure how to do it.

推荐答案

很简单:

var ip = "93.184.216.119";
var host = "example.com";
var ipUri = new UriBuilder(Uri.UriSchemeHttp, ip).Uri;

var request = WebRequest.CreateHttp(ipUri);
request.Host = host;

using (var response = request.GetResponse())
{
    // do something with response
}

这篇关于使用具有已知 IP 端点的 System.Net.WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆