将System.Net.WebRequest与已知的IP端点一起使用 [英] Using System.Net.WebRequest with a known IP end point

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

问题描述

我有大量的DNS名称集合,这些名称已经解析为IP地址.有了这个集合,我需要从他们那里下载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
}

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

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