orouit

Hi all,

I've a tricky problem using HttpWebRequest to place a GET request.

In my hosts file I defined some names for few IP addresses that are on a local Network adapter.

127.0.0.1 localhost
192.168.254.1 xs.pc
192.168.253.1 xs.card
127.0.0.1 xs.simu

If in my request I give the IP address it works normally, however if I give the alias name, like xs.pc, it doesn't work. The device never gets the packets.

The line of code looks like this:

HttpWebRequest httpRequest = (HttpWebRequest) HttpWebRequest.Create(new Uri("http://xs.pc/ewd/login/all")); -> Doesn't work

HttpWebRequest httpRequest = (HttpWebRequest) HttpWebRequest.Create(new Uri("http://192.168.252.1/ewd/login/all")); -> Does work

localhost seems different because even if I remove it, it still works!!

What I don't understand is that it works with the ping command, the browser or an XmlHttpRequest placed from a Firefox toolbar.

I searched in the HttpWebRequest class but I didn't find anything to configure this.

Does anyone knows how to fix this problem

Thks by advance
Olivier




Re: .NET Framework Networking and Communication host name from HOSTS file not resolved with HttpWebRequest

Alan J. McFarlane

Try to narrow down whether its a general .NET or a HttpWebRequest issue.  For instance call Dns.Resolve, or erm the non-obsoleted Dns.GetHostAddresses, and see if you get a result.






Re: .NET Framework Networking and Communication host name from HOSTS file not resolved with HttpWebRequest

Durgaprasad Gorti

First you should get a system.net trace file. Look at my blog article
http://blogs.msdn.com/dgorti for details.

1) When you use an IPAddress, it is possible that you are going to the server directly
without using a proxy. The request would look something like
System.Net.Sockets Verbose: 0 : [4176] Data from Socket#56200037::Send
System.Net.Sockets Verbose: 0 : [4176] 00000000 : 47 45 54 20 2F 20 48 54-54 50 2F 31 2E 31 0D 0A : GET / HTTP/1.1..
System.Net.Sockets Verbose: 0 : [4176] 00000010 : 48 6F 73 74 3A 20 31 37-32 2E 33 31 2E 38 32 2E : Host: 172.31.82.
System.Net.Sockets Verbose: 0 : [4176] 00000020 : 38 33 0D 0A 43 6F 6E 6E-65 63 74 69 6F 6E 3A 20 : 83..Connection:
System.Net.Sockets Verbose: 0 : [4176] 00000030 : 4B 65 65 70 2D 41 6C 69-76 65 0D 0A 0D 0A : Keep-Alive....
System.Net.Sockets Verbose: 0 : [4176] Exiting Socket#56200037::Send() -> 62#62

2) When you use xs.pc, the proxy configuration in your company, specifically if you are using autodetection
would determine that this is an external site and the requets is sent to the proxy. The proxy then resolves the
xs.pc which can't be found, since xs.pc is not set in the Proxy machine's hostname file.
The requets would look like
System.Net.Sockets Verbose: 0 : [4748] 00000000 : 47 45 54 20 68 74 74 70-3A 2F 2F 78 73 2E 70 63 : GET http://xs.pc
System.Net.Sockets Verbose: 0 : [4748] 00000010 : 2F 20 48 54 54 50 2F 31-2E 31 0D 0A 48 6F 73 74 : / HTTP/1.1..Host
System.Net.Sockets Verbose: 0 : [4748] 00000020 : 3A 20 78 73 2E 70 63 0D-0A 50 72 6F 78 79 2D 43 : : xs.pc..Proxy-C
System.Net.Sockets Verbose: 0 : [4748] 00000030 : 6F 6E 6E 65 63 74 69 6F-6E 3A 20 4B 65 65 70 2D : onnection: Keep-
System.Net.Sockets Verbose: 0 : [4748] 00000040 : 41 6C 69 76 65 0D 0A 0D-0A

Consequently the request would fail.

If this is an internal host, set the Proxy to null like
WebRequest.Proxy = null or WebClient.Proxy = null

In any case your next step is to obtain a netmon or system.net trace.






Re: .NET Framework Networking and Communication host name from HOSTS file not resolved with HttpWebRequest

orouit

Thks Alan,

I tried using this:

IPAddress[] IpAddress = Dns.GetHostAddresses(host);

where host is the name I wanted to use (xs.pc for ex) and it worked! In the IpAddress instance I get the correct value. So just before calling HttpWebRequest I just need to call this method!

/Olivier






Re: .NET Framework Networking and Communication host name from HOSTS file not resolved with HttpWebRequest

RBen

I think I understood what you go to work here, so you would have to do this

in my hosts file I have:

1.2.3.4 www.test.com

then in code:

string ipString = "www.test.com";

string url = http://www.test.com/id/test.aspx;

IPAddress[] IpAddress = Dns.GetHostAddresses(ipString);

wRemote = WebRequest.Create(url);

And that should work correctly





Re: .NET Framework Networking and Communication host name from HOSTS file not resolved with HttpWebRequest

Alan J. McFarlane

I think you need to do:

string ipString = "www.test.com";
string urlOriginal =
http://www.test.com/id/test.aspx;
IPAddress[] ipAddresses = Dns.GetHostAddresses(ipString);
UriBuilder bldr = new UriBuilder(urlOriginal);
bldr.Host = ipAddresses[0].ToString();
Uri urlUsingAddress = bldr.Uri;
wRemote = WebRequest.Create(urlUsingAddress);

Untested, uncompiled...

Ideally if the ipAddresses array is longer than one, then you'd loop to the next entry if the connection fails with the current entry.






Re: .NET Framework Networking and Communication host name from HOSTS file not resolved with HttpWebRequest

Mariya Atanasova - MSFT

First, in the example you have given you say that http://192.168.252.1/ewd/login/all succeeds but in the host file you have
192.168.254.1 xs.pc. So xs.pc will resolve to 192.168.254.1.

That being said, HttpWebRequest uses Dns.GetHostEntry to resolve the host, so you may get a different result from Dns.GetHostAddresses. If you want to call HttpWebRequest with the IP address only and Dns.GetHostAddresses works better for you, then you can do the following

IPHostEntry he = Dns.GetHostAddresses("hostname");
foreach (IPAddress ip in he)
{
String uri = "http://" + ip.ToString() + "/ewd/login/all";
HttpWebRequest req;
HttpWebResponse resp;

try
{
req = (HttpWebRequest)WebRequest.Create(url);
resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{
//Do what you want to do
break;
}

if (resp != null) resp.Close();
}

catch (Exception ex)
{
//your code here
}
finally
{
if (resp != null) resp.Close();
}

There is no need to use UriBuilder to build the Uri. That functionality is already incorporeated into HttpWebRequest. It is enough to give it a uri string and it will do the parsing for you

Hope this helps
Mariya





Re: .NET Framework Networking and Communication host name from HOSTS file not resolved with HttpWebRequest

orouit

Thks a lot for all your information,

It works perfectly now and all those posts answered to my request!!

Rgds
Olivier