同一端口上的IIS绑定,但不同的主机名基于http路由
Host
header
. 这里最好的解决方案是配置本地DNS,以便请求
www.tedsoft.com
不要离开机器也就是说,如果这些类型的配置不是一个选项,您可以很容易地将主机头设置为
HttpRequestMessage
.
我在IIS上配置了2个测试站点。
-
默认网站-返回文本“test1”
-
默认网站2-返回文本“test2”
以下代码使用
http://127.0.0.1
(
http://localhost
也可以)并根据IIS绑定适当地设置主机头,以获得您要查找的结果。
class Program
{
static HttpClient httpClient = new HttpClient();
static void Main(string[] args)
{
string test1 = GetContentFromHost("test1"); // gets content from Default Web Site - "test1"
string test2 = GetContentFromHost("test2"); // gets content from Default Web Site 2 - "test2"
}
static string GetContentFromHost(string host)
{
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, "http://127.0.0.1");
msg.Headers.Add("Host", host);
return httpClient.SendAsync(msg).Result.Content.ReadAsStringAsync().Result;
}
}