0

I am coding a PCL Web Lib calling for Windows Phone 8.1 RT and Windows 8.1 app

While HttpWebRequest Class can NOT modify HOST, referer and so on headers running is Windows RT 8.1 app (WP8.1 RT runs OK, it is a bug in Microsoft)

So I want to use HttpClient Class. One UnResolve issue is how to modify request HOST? I can set different HOST without any exception. But actually, the request HOST is not applied. below is my code:

 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();
Jimmy.Wang
  • 115
  • 1
  • 9

1 Answers1

0

According to Wikipedia the host HTTP header is used to identify the virtual host on the server. I think you should set the IP address on the RequestUri and set the host to the desired domain name, for example:

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();
Rico Suter
  • 11,548
  • 6
  • 67
  • 93