gpt4 book ai didi

c# - 如何使用 HttpWebRequest 为 SSL 请求设置主机 header 值

转载 作者:太空狗 更新时间:2023-10-29 21:56:04 25 4
gpt4 key购买 nike

我正在尝试使用 System.Net.HttpWebRequest 类对特定 Web 服务器执行 HTTP GET 请求,以获取我们在众多服务器上进行负载平衡的 Web 应用程序。为了实现这一点,我需要能够为请求设置主机 header 值,我已经能够通过使用 System.Net.WebProxy 类来实现这一点。

但是,当我尝试使用 SSL 执行 GET 时,这一切都崩溃了。当我尝试执行此操作时,对 HttpWebRequest.GetResponse 的调用会引发 System.Net.WebException,HTTP 状态代码为 400(错误请求)。

我想通过 HttpWebRequest 实现的目标是可能的,还是我应该寻找替代方法来执行我想要的目标?

这是我一直用来尝试让这一切正常工作的代码:-

using System;
using System.Web;
using System.Net;
using System.IO;

namespace UrlPollTest
{
class Program
{
private static int suffix = 1;
static void Main(string[] args)
{
PerformRequest("http://www.microsoft.com/en/us/default.aspx", "www.microsoft.com");
PerformRequest("https://www.microsoft.com/en/us/default.aspx", "");
PerformRequest("https://www.microsoft.com/en/us/default.aspx", "www.microsoft.com");

Console.WriteLine("Press any key to continue");
Console.ReadKey();
}

static void PerformRequest(string AUrl, string AProxy)
{
Console.WriteLine("Fetching from {0}", AUrl);
try
{
HttpWebRequest request = WebRequest.Create(AUrl) as HttpWebRequest;
if (AProxy != "")
{
Console.WriteLine("Proxy = {0}", AProxy);
request.Proxy = new WebProxy(AProxy);
}

WebResponse response = request.GetResponse();
using (Stream httpStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(httpStream))
{
string s = reader.ReadToEnd();
File.WriteAllText(string.Format("D:\\Temp\\Response{0}.html", suffix++), s);
}
}
Console.WriteLine(" Success");
}
catch (Exception e)
{
Console.WriteLine(" " + e.Message);
}
}
}
}

最佳答案

我以前使用过一种 hacky 方法 - 有一个修改我的“主机”文件的测试 exe,将农场名称的条目注入(inject)到特定服务器的地址(并发出 ipconfig/flushdns).之后,请求应该被路由到正确的服务器,就好像它是那里唯一的服务器一样。

显然这需要管理员访问权限...我将它用作自动冒烟测试的一部分来攻击农场,就好像它是单独的机器一样。

您可能尝试的另一件事是 NLB 上的某些东西,也许是在 TCL 中(在 F5 的情况下)——也许添加 NLB 理解的自定义 header ? (假设它正在进行 SSL 重新签名)。

关于c# - 如何使用 HttpWebRequest 为 SSL 请求设置主机 header 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1236841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com