How to simulate a Host file for the time of one request



I need to access simultaniously multiple instances of a web services with the following Url. The web services is hosted in IIS and has SSL enabled.

Usually, when we do this process manually, we go one by one and update the Windows host file (c:\Windows\System32\drivers\etc\hosts) like this :

I would like to automate the process and do it with some multithreading. So I cannot change the Host file. Is there a way to simulate a host file when we do a HTTP request in C#?


3 ответов


If you know the IP address of the server's SSL endpoint (which isn't necessarily the same as the server's default IP address), then you could just aim you web-service at that? Obviously the SSL check will fail, but you can disable that through code...

ServicePointManager.ServerCertificateValidationCallback += delegate
    return true; // you might want to check some of the certificate detials...
  • I am getting “The request failed with HTTP status 403: Forbidden.”.

    ServicePointManager.ServerCertificateValidationCallback += delegate{ return true; };

    dataUrl = “http://machine1/dataaccess/DataService.asmx”; DataService dataService = new DataService(dataUrl); return dataService.GetData();

    The problem is that without the Host file, I am unable to use this Url : How do I hit a specific machine and bypass SSL?

    Martin07 августа 2009, 22:36

I think you get the same effect by setting the proxy server of that specific request to the IP address of the actual Web server you want to send the request to.


You can change the URL that your request is hitting at runtime, something like this:

svc.Url = "";

So if you create a program that loops through each of your desired servers, just update the URL property directly (that example is taken from WSE 3 based web services).

  • Because SSL looks for the domain, no?

    Martin07 августа 2009, 23:00
  • LOL, I see. You have the same domain each time mapping to a different IP. You COULD write some code that edits the hosts file each time, but be very careful about how you grant permissions to your hosts file if you do that (if someone can hack your hosts file, they can do all kinds of bad things to you). Alternatively, you might consider setting up an HTTP proxy server. Instead of allowing access to hosts (bad) you could programmaticly change the configuration of the proxy server (not so bad). I have used Apache in a similar way (

    Eric J.08 августа 2009, 00:28
  • Я не могу этого сделать ... Иначе SSL не сработает.

    Martin07 августа 2009, 22:32