HttpWebRequest dot working with HOSTS file mapping? - c#

I don't think this is duplicate of System.Net.WebRequest not respecting hosts file since I do not believe my issues to be proxy related (and it's making the request to localhost essentially).
So I've got a C# ASP page that makes an HttpWebRequest. Now, on this server I've done some custom mapping of some dev domains to the server's IP address.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( "http://someproject.dev" );
ERROR:
The remote name could not be resolved: 'someproject.dev' at System.Net.HttpWebRequest.GetResponse()
My Hosts file on the server (and my client) has 10.0.1.115 someproject.dev
I've also tried it with 127.0.0.1 on the server
Another note, both .net apps are running under hosts file/IIS mappings, i.e. one app is making a x-domain request to the other (which is permitted, i know that's not the cause because this works in our production environment).
Also, I'd like to avoid System.Net.Dns, unless you guys agree that's the only way it will work.

I would check your server. Any web calls that are made from the server are checked to see if the DNS is in the hosts file. Sounds like a server problem instead of a programming problem.

Related

How to request an Asp.Net page manually?

I have a project and it contains 2 pages: test1.aspx and test2.aspx. Now from test1.aspx I want to manually request test2.aspx and get the HTML out of it. I could do this using HttpClient or HttpWebRequest. Problem is I have a firewall and I suspect it won't work. Is there any other way to download the content from the webpage without actually using HttpWebRequest
Thanks in advance.
I don't really like what you are trying to do ;) Anyway, since your page don't seems to be a static page (.aspx) you must do a request to your webserver, whatever the method you use (HttpClient or HttpWebRequest).
Usually, a request done on the same machine does not passes through the network. If the DNS alias point to the machine IP address a loopback occurs.
In this case:
if your firewall is somewhere on your network, you don't care about
it, the request will not leave your host
if you speak about a firewall software, on your machine, it may block
the request. You may have to authorize such requests or force the DNS locally in your host file to specify 127.0.0.1 (which is a true localhost) and may work with
most firewall software
if you are on a Windows Server and your site require authentication, you may have to deal with Loopback
Check (or here)
NB: Loopbacks are usually considered as security breach and not recommended.
You should think about another solution like Ajax Web Services, Web or User controls (as already said) etc...

Calling a webservice on one website from another website; both hosted on the same server

I have a server serving multiple websites under one IP with bindings on specific URLs.
However, one of the sites I am hosting needs to pull data off a webservice (.asmx file) located in another IIS website on the same server. The code works fine locally but when we push the code to our server, it seems not to be able to locally resolve the lookup. (getting the dreaded 'No connection could be made because the target machine actively refused it'. I think what's happening is it tries to use the endpoint address and do a DNS lookup, but because the DNS lookup ends up resolving to the same IP, it actually pulls the traffic via in-memory or pipes or something and not network, which causes IIS not to bind to the correct site, and so the whole thing goes to pot.
Is there a way I can use the windows hosts file or other mechanism to fix this apparent DNS issue? Perhaps someone might shed light on this and maybe its not DNS at all. Thanks!
UPDATE - Here is a quick breakdown of the domains in question. Nothing is specifically coded or pointing to 'localhost'. Everyhing is using fully qualified domain names.
DomainA.com is a website that, when a form is submitted, calls a web service from DomainB.com. Both DomainA.com and DomainB.com are 'web sites' set up in IIS on the same server bound to the same external IP address. What I believe to be the problem is that DomainA.com, when the form is submitted, does a DNS lookup for DomainB.com, and when it finds that the domain is the same IP address, does not use the domain name when making the requests to DomainB.com. Without the domain name being used/sent, IIS doesn't know which site to connect to and the 'target machine actively refuses' the connection.
Do you have to location to your webservice in a configuration file?
Take a look at the proxy class you generated to consume it.
The host is usually hardcoded into that file.
If you didn't use your production host to create that file(or service reference), it is pointing to your development webservice. This won't be accessible from your production machine.

Sending Local IP address of connection associated with HttpWebrequest (C#)

I am developing an application which has one web server and one C# client which posts XML to the web server. The web server needs to know the local IP address of client. I tried methods to retrieve the IP address at the server side, but these methods don't give the IP address of the client when there are proxy servers or NAT in between. So I need to find the local IP of the connection at the client application and send it with the request.
The problem is in HttpWebRequest. I don't see any method by which I can get the local IP address of the connection made while sending HTTP request.
CLARIFICATION:
The client in my case is not browser-based. It is a C# application. My server has specific rules based on local IP, that was are used to connect to the server while sending the HTTP request. In my case, local IPs are fixed. There could be multiple IPs on a local machine -- that's why I want the local IP associated with the Socket used to send the request. This can be solved if I use TcpClient in C# and implement the HTTP protocol on top of it, but I want to avoid that. So, is there any way I can get the socket associated with HttpWebRequest before posting the request?
It is not possible to get the local IPAddress of the connection that HttpWebRequest is going to use. However, you could use Dns or System.Net.NetworkInformation class to get all the local IPaddresses, and send them as a custom header with your request. On the server side you could parse this header, and see if any of the IP's match the one that you are expecting.
However, as Damien has indicated above, this solution is not foolproof, people could spoof the IPaddress to the one the server is expecting. Maybe you need to take a step back and think what security you are trying to achive by this? And see if you could accomplish your goals by a different method - for eg: user Authentication using a supported method like Basic(over HTTPS), Digest, NTLM, Kerberos, Negotiate, or use SSL with client certs?
One possible solution is to tell the client what ip it should use when connecting to the server, and include that ip in a custom header for the server to read.
var srcip = IPAddress.Parse("10.0.0.1");
var request = (HttpWebRequest)HttpWebRequest.Create ("http://example.com/");
request.Headers.Add("X-Original-Client-IP", srcip.ToString());
request.ServicePoint.BindIPEndPointDelegate = delegate { return new IPEndPoint(srcip, 0); };
request.GetResponse();
There's no guaranteed way of doing this. Javascript and server-side code can only see the public address, not the local one. There is a method for getting it using Java (mentioned here), but it relies on both Javascript and Java being available, and the user's browser supporting that particular call.
Given that many local IPs are assigned as needed (my home PC's is assigned using DHCP, my work PC's changes occasonally too), do you think knowing the local IP would actually be any use?
Maybe you should move from HttpWebRequest to SOAP requests/responses if XML is what you send over the line. Implement ASPX web service or WFC webservice. HttpWebRequest are always seen to the server as they are coming from the last IP that issued them so that would be NAT or proxy.
However, I too find it odd enough that you need to know local machine IP on the server side.

How can I configure Cassini web server to accept requests to different hosts?

See duplicate: Customizing the cassini webserver
I have a web service hosted using Cassini web server. It works fine if I reference it by localhost, but when I use my machine's name (even using the browser locally), I get:
Server Error in '/' Application.
HTTP Error 403 - Forbidden.
Version Information: Cassini Web Server 1.0.40305.0
How can I configure it to accept all requests?
Cassini is specifically build to reject any outside connections. You could rebuild from sourcecode if you want it to accept outside connections.
Edit:
In reply to the below comment on what to edit in the sourcecode, you'd probably want to change the following in Server.cs on line 88:
_socket = CreateSocketBindAndListen(AddressFamily.InterNetwork,
IPAddress.Loopback, _port);
Into something like this:
_socket = CreateSocketBindAndListen(AddressFamily.InterNetwork,
IPAddress.Any, _port);
This will bind the listening socket to all available IP addresses on the system instead of only to the localhost address (127.0.0.1).
To update this answer, IIS Express is a new alternative that allows this.
http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx
To avoid anyone else having to go further, if you need a lightweight web server for production use, IIS-Express is not recommended (by themselves) for that purpose .

Is it possible to redirect a url to another using a webproxy ( such as fiddler )

I'm trying to parse a WSDL file which is in another server but has hard codded "localhost" all over the document.
When I fetch it, obviously the program complains "connection refused" because nothing is running in my machine.
My question is: Is it possible to use a webproxy ( such as fiddler ) to redirect those localhost request to my other server so the WSDL references are complete?
:-/
Thanks
p.s. I could always have fixed the remote "wsdl" but the guy on charge will be here until next week and I would like to start working today.
You could use Fiddler as the proxy from your machine, and then have it rewrite the WSDL to change localhost to the correct machine name.
The FiddlerScript CookBook has an example on how to write this sort of script. Go to that page and search for "Remove all DIV tags (and content inside the DIV tag)". Just change the regex to match localhost and set the replace to the machine name you want to use.
If you have SSH access to the machine, you should be able to use SSH port forwarding to accomplish this. I'm assuming you're using Windows (based on the C# tag), so you can use Putty as explained here: Using port forwarding with PuTTY. Just follow those instructions to forward the desired port on "localhost" to the server that serves the WSDL.
Alternatively, if you're on a *nix based machine or a Mac, use SSH w/ the following command:
ssh -L PORTYOUWILLUSE:localhost:PORTONSERVER username#serverhostname
For example, if the WSDL were served on port 80, you could do
ssh -L 80:localhost:80 username#server
Once you're logged in (with either method), any requests to localhost on port 80 will be rerouted to the server.
If you only want to change it for a few minutes while you parse the WSDL, you might be able to change the HOST file and point "localhost" to the remote IP address. The hosts file is in "C:\Windows\System32\drivers\etc" in Windows VISTA/XP.
There's a few ways you could achieve this, none are particularly robust as long-term solutions, but you mention you just want something temporary until the dev gets back.
If everything after the domain matches (if your remote URL is otherwise the same as the localhost one), you can edit your localhost entry in your hosts file.
In system32\drivers\etc, copy the "hosts" file onto your desktop. Open in notepad and change this line:
127.0.0.1 localhost
Change the IP address (127.0.0.1) to your remote domain. Then copy the hosts file back into the etc directory. (Note: it's not possible to edit this file directly, as Administrator or otherwise).
If you have multiple domains on the remote web service, in IIS, you need to change the web site to serve for requests to "localhost", this might seem a bit odd, but it'll work because your machine will make requests to the server's IP address, but specify the request domain as "localhost". Right-click the website in IIS and select properties, then add the domain "localhost" to the list of HTTP-Header values supported by that web site. You can ignore all of this if your web site in IIS will serve content if you access it via an IP address. If that single IP address is shared between multiple web sites (which is usually the case), you'll get a "Bad hostname" error from IIS as it attempts to look up "localhost" and can't find which web site to direct the request to.
Another possibility is to use a personal proxy server called Proxomitron. It's a little old, and no longer under development, but it's very easy to setup and very solid.
Once you've installed it, open it and click "Config" - change the port it listens on to 80. Next you need to create a redirect rule (it's not actually a redirect, more of a rewrite of the url). You'll need to have a quick read of the docs to understand how to add your own redirect, but there's plenty of samples that ship with the app. The rule you're looking for is RDIR:
$RDIR( ) Is more sneaky and redirects the connection in Proxomitron without telling your browser. This is useful when you want your browser to think it going one place when, in reality, it's going somewhere else.

Categories

Resources