我正在编写一个PCL Web Lib,用于调用Windows Phone 8.1 RT和Windows 8.1应用程序
虽然HttpWebRequest Class无法修改HOST,但是referer等运行的标头是Windows RT 8.1应用程序(WP8.1 RT运行正常,这是微软的一个错误)
所以我想使用HttpClient Class。 一个UnResolve问题是如何修改请求HOST?我可以设置不同的HOST而没有任何异常。但实际上,请求HOST未应用。下面是我的代码:
var client = new HttpClient();
var request = new HttpRequestMessage()
{
RequestUri = new Uri("http://www.bing.com"),
Method = HttpMethod.Get,
};
client.DefaultRequestHeaders.Host = new HostName("12.34.56.78");
//client.DefaultRequestHeaders.Add("Host", "12.34.56.78");
var respose = await client.SendRequestAsync(request);
var content = await respose.Content.ReadAsStringAsync();
答案 0 :(得分:0)
根据Wikipedia,主机HTTP标头用于标识服务器上的虚拟主机。我认为您应该在RequestUri
上设置IP地址,并将主机设置为所需的域名,例如:
var client = new HttpClient();
var request = new HttpRequestMessage()
{
RequestUri = new Uri("http://12.34.56.78/path"),
Method = HttpMethod.Get,
};
client.DefaultRequestHeaders.Host = new HostName("www.bing.com");
var respose = await client.SendRequestAsync(request);
var content = await respose.Content.ReadAsStringAsync();