【问题标题】:Building an HTTPPing that can overide a Host File构建可以覆盖主机文件的 HTTPPing
【发布时间】:2012-02-28 00:20:40
【问题描述】:

我希望构建一个 HTTPPing 实用程序,它可以绕过我们的负载平衡器并在我们的 SharePoint 环境中测试每个前端。编写(或查找)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 个更改,我认为有更好的方法来做到这一点。我希望程序不编辑主机文件。

谢谢,蒂姆

【问题讨论】:

  • 为什么不临时为 IP 地址添加 AAM?

标签: c# .net sharepoint http


【解决方案1】:

您可以使用反射来欺骗主机标头:

        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. 他的回答。

【讨论】:

    【解决方案2】:

    这样的事情怎么样:

    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");
    

    我们使用这样的东西正是为了您所追求的目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      相关资源
      最近更新 更多