开发者

HttpWebRequest to different IP than the domain resolves to

开发者 https://www.devze.com 2022-12-23 13:00 出处:网络
Long story short an API I\'m calling\'s different environments (dev/staging/uat/live) is set up by putting a host-record on the server so the live domain resolves to their other server in for the HTTP

Long story short an API I'm calling's different environments (dev/staging/uat/live) is set up by putting a host-record on the server so the live domain resolves to their other server in for the HTTP request.

The problem is that they've done this with so many different environments that we don't have enough servers to use the server-wide host files for it anymore (We've got some environments running off the same servers - luckily not dev and live though :P).

I'm wondering if there's a way to make WebRequest request to a domain but explicitly specify the IP of the server it should connect to? Or is there any way of doing this short of going all the way down to socket connections (Which I'd really prefer not to waste time/create bugs by trying to re-implementing the HTTP protocol).

PS: I've tried and we can't just get a new sub-domain for each environment.开发者_运维百科


One way to spoof a HTTP host header is to set a proxy to the actual server you'd like the request sent to. Something like

request.Proxy = new WebProxy(string.Format("http://{0}/", hostAddress));

may well work.


There are ways to control configuration values.

  1. You have conditional compilation enabled in .NET, in which you can create your configuration sets and create directives that can use specific domain instead of changing its resolution strategy. For example, in debug, you can use x.com and in release mode you can use y.com, wherever you need to reference your url.
  2. Web.config and app.config now supports changes as per the configuration selected, you can have web.debug.config and web.release.config and you can specify different url references here.
0

精彩评论

暂无评论...
验证码 换一张
取 消