我希望构建一个可以绕过负载均衡器并在我们的SharePoint环境中测试每个前端的HTTPPing实用程序。编写(或找到) HTTPPing并不困难,它是让它在我们的环境中运行。
在我们的SharePoint环境中,我们有五个web应用程序在负载均衡器后面运行,负载均衡器在10个前端之间进行平衡。五个web应用程序中的每一个都通过映射到虚拟IP的DNS条目进行访问,因此:
webapp1.mycompany.com --> 10.20.30.10
webapp2.mycompany.com --> 10.20.30.11
webapp3.mycompany.com --> 10.20.30.12
webapp4.mycompany.com --> 10.20.30.13
webapp5.mycompany.com --> 10.20.30.14前端计算机的IP地址可能是:
Front End 1 --> 10.22.33.40
Front End 2 --> 10.22.33.41
...
Front End 10 --> 10.22.33.49我们的SharePoint环境使用备用访问映射,所以我不能使用http://10.22.33.40/,但必须使用http://webapp1.mycompany.com
如果我将我的主机文件更改为包含以下条目,则可以直接访问计算机:
webapp1.mycompany.com --> 10.22.33.40但是5个web应用程序x 10个前端意味着50个变化,我认为有更好的方法来做到这一点。我希望程序不要编辑主机文件。
谢谢,蒂姆
发布于 2012-02-06 22:52:50
您可以使用反射欺骗主机标头:
WebRequest wr = WebRequest.Create(@"http://127.0.0.1/Test.html");
wr.Headers.GetType().InvokeMember("ChangeInternal", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, wr.Headers, new object[] { "Host", "www.example.com" });
var resp = wr.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
MessageBox.Show(sr.ReadToEnd().ToString());感谢L.B.的回答。
发布于 2012-02-06 23:16:04
这样如何:
public static bool Ping(string url, string actualTargetEnpoint)
{
var uri = new UriBuilder(url);
// take note of the original host to use for the "Host" header
var originalHost = uri.Host;
// swap out the actual endpoint we are going to be hitting
uri.Host = actualTargetEnpoint;
var req = (HttpWebRequest)WebRequest.Create(uri.ToString());
// replace the host header on the request for the originally supplied target
req.Host = originalHost;
var response = (HttpWebResponse)req.GetResponse();
return response.StatusCode == HttpStatusCode.OK;
}.Net 4允许您直接操作主机。这意味着您可以轻松地将您的web请求指向新的端点,同时保持原始的主机头。不需要主机文件更改或特殊的代理配置等。就像这样调用:
var success = Ping("http://webapp1.mycompany.com/", "10.20.30.10");我们用这样的东西来达到你想要的目的。
https://stackoverflow.com/questions/9161575
复制相似问题