Connecting Tech Pros Worldwide Forums | Help | Site Map

How can I get the IP address of the host Web Server into a Session variable?

John Kotuby
Guest
 
Posts: n/a
#1: Jul 11 '08
Hi all,

I have a situation where a Web Server at an install site does not currently
have a publically registered Domain Name associated with the site. This
requires that the a user access the site from a remote (outside the LAN)
browser using the IP address, as in http://192.35.78.234 .

Currently in my code I use the following syntax to obtain the Root Web path
which I then append as a prefix to Images and Links in external documents
(Excel files) so the recipient of an HTML Email or one of the spreadsheets
can both resolve the images or click on a spreadsheet link to view a page on
the site.

Dim strRootWeb As String = Me.Request.Headers.Item("Host").ToString
strRootWeb = "http://" + strRootWeb
Session.Add("RootWeb", strRootWeb)

Then for a working link in an external document I will do something like:

Public strRootWeb = Session("RootWeb")
strRootWeb & "/Common/PageView.aspx

But this does not work if the name of the Web Server is not publically
registered with DNS servers.

Thanks for any help on resolving the IP address of a Web Server root.



Mark Rae [MVP]
Guest
 
Posts: n/a
#2: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


"John Kotuby" <jkotuby75@comcast.netwrote in message
news:%231wSDD34IHA.2348@TK2MSFTNGP06.phx.gbl...

Not sure if this is what you want or not but, based on the subject of your
post, you can get the IP address of the host web server into a Session
variable like this:

Session["HostIP"] = Request.ServerVariables["LOCAL_ADDR"].ToString();


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

John Kotuby
Guest
 
Posts: n/a
#3: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


Thanks for the reply Mark,

I think that will give me the actual IP address of the Server box that the
Web Application is running on. That would work for me if the was directly
connected to the Internet.
However, in this situation, the Web Server is behind a firewall and being
accessed via NAT.

What I am really looking for is the Public IP Address that is used in the
request to get to the Router that then finds the Server behind the firewall.

I am thinking that it is contained somewhere in the HttpRequest object just
as the Me.Request.Headers.Item("Host").ToString returns the string for the
Root Web as requested by the client.

When the browser sends out a request for http://www.url.com that is
immediately converted by a public DNS server to an IP address. It is that
public IP address that I seek to grab, not matter what Web server is hosting
our application. I have been looking for the property that contains the IP
address but can't seem to find it in the documentation.

Thanks...


"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:ecsxbj34IHA.4916@TK2MSFTNGP06.phx.gbl...
Quote:
"John Kotuby" <jkotuby75@comcast.netwrote in message
news:%231wSDD34IHA.2348@TK2MSFTNGP06.phx.gbl...
>
Not sure if this is what you want or not but, based on the subject of your
post, you can get the IP address of the host web server into a Session
variable like this:
>
Session["HostIP"] = Request.ServerVariables["LOCAL_ADDR"].ToString();
>
>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

=?Utf-8?B?YnJ1Y2UgYmFya2Vy?=
Guest
 
Posts: n/a
#4: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


if the firewall (or load balancer) is doing nat translation (which it should
be), then its not available (as only the firewall knows it).

your best bet is to add to the ipaddress to the web.config as a setting

-- bruce (sqlwork.com)


"John Kotuby" wrote:
Quote:
Thanks for the reply Mark,
>
I think that will give me the actual IP address of the Server box that the
Web Application is running on. That would work for me if the was directly
connected to the Internet.
However, in this situation, the Web Server is behind a firewall and being
accessed via NAT.
>
What I am really looking for is the Public IP Address that is used in the
request to get to the Router that then finds the Server behind the firewall.
>
I am thinking that it is contained somewhere in the HttpRequest object just
as the Me.Request.Headers.Item("Host").ToString returns the string for the
Root Web as requested by the client.
>
When the browser sends out a request for http://www.url.com that is
immediately converted by a public DNS server to an IP address. It is that
public IP address that I seek to grab, not matter what Web server is hosting
our application. I have been looking for the property that contains the IP
address but can't seem to find it in the documentation.
>
Thanks...
>
>
"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:ecsxbj34IHA.4916@TK2MSFTNGP06.phx.gbl...
Quote:
"John Kotuby" <jkotuby75@comcast.netwrote in message
news:%231wSDD34IHA.2348@TK2MSFTNGP06.phx.gbl...

Not sure if this is what you want or not but, based on the subject of your
post, you can get the IP address of the host web server into a Session
variable like this:

Session["HostIP"] = Request.ServerVariables["LOCAL_ADDR"].ToString();


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
>
>
>
Mark Rae [MVP]
Guest
 
Posts: n/a
#5: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


"John Kotuby" <jkotuby75@comcast.netwrote in message
news:eVYboS44IHA.4272@TK2MSFTNGP03.phx.gbl...
Quote:
What I am really looking for is the Public IP Address that is used in the
request to get to the Router that then finds the Server behind the
firewall.
Hmm - if the box was connected to the Internet, it would be simple enough to
fetch its IP address as seen by the outside world by inspecting the
HttpResponse returned from something like: http://www.whatismyip.org/

However, as Bruce has explained, because of the NAT and firewall in the
equation it's going to be pretty much impossible to determine this in this
case...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

John Kotuby
Guest
 
Posts: n/a
#6: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


Thanks guys, I was hoping to find the requested public IP address in the
Request object somewhere, but apparently it is not to be found.

However gathering the response from http://www.whatismyip.org/ might just
work after all.

I am running a workstation that is behind a Gateway Firewall device and it
uses NAT. When I click on the whatismyip link above I get the public IP
Address that I must use to connect to my workstation from home via RDP
(using a non-standard Port of course).

So if I am getting the Public IP of a NATed workstation, why wouldn't that
technique work for a NATed Web Server...it's just another box? In fact any
response from another public site can ONLY return the public IP address of a
NATed box. Brilliant!

I am going brain-dead today because I can't even recall how to get the
HttpResponse returned from something like: http://www.whatismyip.org/ in my
code-behind or maybe in the Global.asax itself.

I will resort to hard-coding the IP Address in web.config or global.asax if
necessary, as that is not too difficult, but I see possibly 30 or more
deployments that might need this sort of thing.

Thanks again...you guys are always helpful.


"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:uq9bet44IHA.1952@TK2MSFTNGP03.phx.gbl...
Quote:
"John Kotuby" <jkotuby75@comcast.netwrote in message
news:eVYboS44IHA.4272@TK2MSFTNGP03.phx.gbl...
>
Quote:
>What I am really looking for is the Public IP Address that is used in the
>request to get to the Router that then finds the Server behind the
>firewall.
>
Hmm - if the box was connected to the Internet, it would be simple enough
to fetch its IP address as seen by the outside world by inspecting the
HttpResponse returned from something like: http://www.whatismyip.org/
>
However, as Bruce has explained, because of the NAT and firewall in the
equation it's going to be pretty much impossible to determine this in this
case...
>
>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mark Rae [MVP]
Guest
 
Posts: n/a
#7: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


"John Kotuby" <jkotuby75@comcast.netwrote in message
news:OLT7g%2344IHA.964@TK2MSFTNGP05.phx.gbl...
Quote:
I am going brain-dead today because I can't even recall how to get the
HttpResponse returned from something like: http://www.whatismyip.org/ in
my code-behind or maybe in the Global.asax itself.
string strHostIP = String.Empty;
using (WebClient objWebClient = new WebClient())
{
using (StreamReader objStreamReader = new
StreamReader(objWebClient.OpenRead("http://www.whatismyip.org/")))
{
strHostIP = objStreamReader.ReadToEnd();
objStreamReader.Close();
}
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

John Kotuby
Guest
 
Posts: n/a
#8: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


Well, I think I'm getting closer, but maybe just chasing my tail.
I converted your code example to VB and here is what is ending up in my
Session variable.

Error: blank User-Agent header is not allowed! Please ask the author of your
IP address checking client to specify their client's name in the User-Agent
header

Maybe it's something I failed to do when creating the WebClient?
From the same machine that I am using as the Web Server, still I get just
the correct IP address in my Web Browser and that is ALL that is being sent
back.
Why should the web browser be any different than the WebClient?
Oh well...It's 5 oclock on a Friday. I knew it had to be 5 oclock somehwere!

"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:em7BXL54IHA.1952@TK2MSFTNGP02.phx.gbl...
Quote:
"John Kotuby" <jkotuby75@comcast.netwrote in message
news:OLT7g%2344IHA.964@TK2MSFTNGP05.phx.gbl...
>
Quote:
>I am going brain-dead today because I can't even recall how to get the
>HttpResponse returned from something like: http://www.whatismyip.org/ in
>my code-behind or maybe in the Global.asax itself.
>
string strHostIP = String.Empty;
using (WebClient objWebClient = new WebClient())
{
using (StreamReader objStreamReader = new
StreamReader(objWebClient.OpenRead("http://www.whatismyip.org/")))
{
strHostIP = objStreamReader.ReadToEnd();
objStreamReader.Close();
}
}
>
>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mark Rae [MVP]
Guest
 
Posts: n/a
#9: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


"John Kotuby" <jkotuby75@comcast.netwrote in message
news:elP7St54IHA.5060@TK2MSFTNGP02.phx.gbl...

[top-posting corrected as usual]
Quote:
I converted your code example to VB
Why on earth did you do that...?
Quote:
Error: blank User-Agent header is not allowed! Please ask the author of
your IP address checking client to specify their client's name in the
User-Agent header
Use this URL instead: http://www.netikus.net/show_ip.html


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jamal
Guest
 
Posts: n/a
#10: Jul 11 '08

re: How can I get the IP address of the host Web Server into a Session variable?


Try the following:

Private Sub MyIPaddressButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyIPaddressButton.Click
Dim strHostIP As String = String.Empty

Using objWebClient As New WebClient()

Using objStreamReader As StreamReader = New
StreamReader(objWebClient.OpenRead
("http://www.whatismyip.com/automation/n09230945.asp"))

strHostIP = objStreamReader.ReadToEnd()

objStreamReader.Close()

End Using

End Using

End Sub

Jamal

"John Kotuby" <jkotuby75@comcast.netwrote in message
news:elP7St54IHA.5060@TK2MSFTNGP02.phx.gbl...
Quote:
Well, I think I'm getting closer, but maybe just chasing my tail.
I converted your code example to VB and here is what is ending up in my
Session variable.
>
Error: blank User-Agent header is not allowed! Please ask the author of
your IP address checking client to specify their client's name in the
User-Agent header
>
Maybe it's something I failed to do when creating the WebClient?
From the same machine that I am using as the Web Server, still I get just
the correct IP address in my Web Browser and that is ALL that is being
sent back.
Why should the web browser be any different than the WebClient?
Oh well...It's 5 oclock on a Friday. I knew it had to be 5 oclock
somehwere!
>
"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:em7BXL54IHA.1952@TK2MSFTNGP02.phx.gbl...
Quote:
>"John Kotuby" <jkotuby75@comcast.netwrote in message
>news:OLT7g%2344IHA.964@TK2MSFTNGP05.phx.gbl...
>>
Quote:
>>I am going brain-dead today because I can't even recall how to get the
>>HttpResponse returned from something like: http://www.whatismyip.org/ in
>>my code-behind or maybe in the Global.asax itself.
>>
>string strHostIP = String.Empty;
>using (WebClient objWebClient = new WebClient())
>{
> using (StreamReader objStreamReader = new
>StreamReader(objWebClient.OpenRead("http://www.whatismyip.org/")))
> {
> strHostIP = objStreamReader.ReadToEnd();
> objStreamReader.Close();
> }
>}
>>
>>
>--
>Mark Rae
>ASP.NET MVP
>http://www.markrae.net
>
>

=?Utf-8?B?YnJ1Y2UgYmFya2Vy?=
Guest
 
Posts: n/a
#11: Jul 12 '08

re: How can I get the IP address of the host Web Server into a Session variable?


if the firewall is configured correctly, this will not work, as the webserver
will not be able to access an internet dns server (ports blocked), which is
required to translate the dns name to an ipaddress.

the firewall (say its f5) may have an api your code can call.

-- bruce (sqlwork.com)


"Jamal" wrote:
Quote:
Try the following:
>
Private Sub MyIPaddressButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyIPaddressButton.Click
Dim strHostIP As String = String.Empty
>
Using objWebClient As New WebClient()
>
Using objStreamReader As StreamReader = New
StreamReader(objWebClient.OpenRead
("http://www.whatismyip.com/automation/n09230945.asp"))
>
strHostIP = objStreamReader.ReadToEnd()
>
objStreamReader.Close()
>
End Using
>
End Using
>
End Sub
>
Jamal
>
"John Kotuby" <jkotuby75@comcast.netwrote in message
news:elP7St54IHA.5060@TK2MSFTNGP02.phx.gbl...
Quote:
Well, I think I'm getting closer, but maybe just chasing my tail.
I converted your code example to VB and here is what is ending up in my
Session variable.

Error: blank User-Agent header is not allowed! Please ask the author of
your IP address checking client to specify their client's name in the
User-Agent header

Maybe it's something I failed to do when creating the WebClient?
From the same machine that I am using as the Web Server, still I get just
the correct IP address in my Web Browser and that is ALL that is being
sent back.
Why should the web browser be any different than the WebClient?
Oh well...It's 5 oclock on a Friday. I knew it had to be 5 oclock
somehwere!

"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:em7BXL54IHA.1952@TK2MSFTNGP02.phx.gbl...
Quote:
"John Kotuby" <jkotuby75@comcast.netwrote in message
news:OLT7g%2344IHA.964@TK2MSFTNGP05.phx.gbl...
>
>I am going brain-dead today because I can't even recall how to get the
>HttpResponse returned from something like: http://www.whatismyip.org/ in
>my code-behind or maybe in the Global.asax itself.
>
string strHostIP = String.Empty;
using (WebClient objWebClient = new WebClient())
{
using (StreamReader objStreamReader = new
StreamReader(objWebClient.OpenRead("http://www.whatismyip.org/")))
{
strHostIP = objStreamReader.ReadToEnd();
objStreamReader.Close();
}
}
>
>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
>
>
>
John Kotuby
Guest
 
Posts: n/a
#12: Jul 14 '08

re: How can I get the IP address of the host Web Server into a Session variable?


Mark,
That site actually worked without changing any of my code. Thank you.

Sure, why should anyone use VB when C# is far superior. I suppose I could
have placed your code in Script tags and designated the language as C#. The
rest of the project is written in VB so I though I would try to maintain
continuity. I actually have not tried mixing C# and VB in the same project
(primarily because I am not too familiar with C#).

Also I guess my firewall is not set up properly because other posters have
said this should not work. I am not sure what they are driving at, but all I
see is a call out through port 80 which is usually left open at most sites
for Web surfing.

Have a good one...

"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:%23hoOn054IHA.3484@TK2MSFTNGP05.phx.gbl...
Quote:
"John Kotuby" <jkotuby75@comcast.netwrote in message
news:elP7St54IHA.5060@TK2MSFTNGP02.phx.gbl...
>
[top-posting corrected as usual]
>
Quote:
>I converted your code example to VB
>
Why on earth did you do that...?
>
Quote:
>Error: blank User-Agent header is not allowed! Please ask the author of
>your IP address checking client to specify their client's name in the
>User-Agent header
>
Use this URL instead: http://www.netikus.net/show_ip.html
>
>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

John Kotuby
Guest
 
Posts: n/a
#13: Jul 14 '08

re: How can I get the IP address of the host Web Server into a Session variable?


Thanks Jamal, that seems to work also.

"Jamal" <®ivodotnet®i@®iyahoo.comwrote in message
news:4877db80$0$5024$607ed4bc@cv.net...
Quote:
Try the following:
>
Private Sub MyIPaddressButton_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyIPaddressButton.Click
Dim strHostIP As String = String.Empty
>
Using objWebClient As New WebClient()
>
Using objStreamReader As StreamReader = New
StreamReader(objWebClient.OpenRead
("http://www.whatismyip.com/automation/n09230945.asp"))
>
strHostIP = objStreamReader.ReadToEnd()
>
objStreamReader.Close()
>
End Using
>
End Using
>
End Sub
>
Jamal
>
"John Kotuby" <jkotuby75@comcast.netwrote in message
news:elP7St54IHA.5060@TK2MSFTNGP02.phx.gbl...
Quote:
>Well, I think I'm getting closer, but maybe just chasing my tail.
>I converted your code example to VB and here is what is ending up in my
>Session variable.
>>
>Error: blank User-Agent header is not allowed! Please ask the author of
>your IP address checking client to specify their client's name in the
>User-Agent header
>>
>Maybe it's something I failed to do when creating the WebClient?
>From the same machine that I am using as the Web Server, still I get just
>the correct IP address in my Web Browser and that is ALL that is being
>sent back.
>Why should the web browser be any different than the WebClient?
>Oh well...It's 5 oclock on a Friday. I knew it had to be 5 oclock
>somehwere!
>>
>"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
>news:em7BXL54IHA.1952@TK2MSFTNGP02.phx.gbl...
Quote:
>>"John Kotuby" <jkotuby75@comcast.netwrote in message
>>news:OLT7g%2344IHA.964@TK2MSFTNGP05.phx.gbl...
>>>
>>>I am going brain-dead today because I can't even recall how to get the
>>>HttpResponse returned from something like: http://www.whatismyip.org/
>>>in my code-behind or maybe in the Global.asax itself.
>>>
>>string strHostIP = String.Empty;
>>using (WebClient objWebClient = new WebClient())
>>{
>> using (StreamReader objStreamReader = new
>>StreamReader(objWebClient.OpenRead("http://www.whatismyip.org/")))
>> {
>> strHostIP = objStreamReader.ReadToEnd();
>> objStreamReader.Close();
>> }
>>}
>>>
>>>
>>--
>>Mark Rae
>>ASP.NET MVP
>>http://www.markrae.net
>>
>>
>
>

John Kotuby
Guest
 
Posts: n/a
#14: Jul 14 '08

re: How can I get the IP address of the host Web Server into a Session variable?


Bruce,
My firewall must not be set up properly, as the above solutions worked. I
don't know how port blocking would interfere here as it appears to me we are
just using port 80 which is left open at most sites for web surfing. I am
trying to get the public IP address of the Web Server outside the firewall,
not the internal LAN address.

Please explain, if you have a minute, because now I am concerned about our
firewall here.
Thanks...

"bruce barker" <brucebarker@discussions.microsoft.comwrote in message
news:7611F14A-65FF-4DB7-B634-F0E7FFB5FDDB@microsoft.com...
Quote:
if the firewall is configured correctly, this will not work, as the
webserver
will not be able to access an internet dns server (ports blocked), which
is
required to translate the dns name to an ipaddress.
>
the firewall (say its f5) may have an api your code can call.
>
-- bruce (sqlwork.com)
>
>
"Jamal" wrote:
>
Quote:
>Try the following:
>>
>Private Sub MyIPaddressButton_Click(ByVal sender As System.Object, ByVal
>e
>As System.EventArgs) Handles MyIPaddressButton.Click
>Dim strHostIP As String = String.Empty
>>
>Using objWebClient As New WebClient()
>>
> Using objStreamReader As StreamReader = New
>StreamReader(objWebClient.OpenRead
>("http://www.whatismyip.com/automation/n09230945.asp"))
>>
> strHostIP = objStreamReader.ReadToEnd()
>>
> objStreamReader.Close()
>>
> End Using
>>
>End Using
>>
>End Sub
>>
>Jamal
>>
>"John Kotuby" <jkotuby75@comcast.netwrote in message
>news:elP7St54IHA.5060@TK2MSFTNGP02.phx.gbl...
Quote:
Well, I think I'm getting closer, but maybe just chasing my tail.
I converted your code example to VB and here is what is ending up in my
Session variable.
>
Error: blank User-Agent header is not allowed! Please ask the author of
your IP address checking client to specify their client's name in the
User-Agent header
>
Maybe it's something I failed to do when creating the WebClient?
From the same machine that I am using as the Web Server, still I get
just
the correct IP address in my Web Browser and that is ALL that is being
sent back.
Why should the web browser be any different than the WebClient?
Oh well...It's 5 oclock on a Friday. I knew it had to be 5 oclock
somehwere!
>
"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:em7BXL54IHA.1952@TK2MSFTNGP02.phx.gbl...
>"John Kotuby" <jkotuby75@comcast.netwrote in message
>news:OLT7g%2344IHA.964@TK2MSFTNGP05.phx.gbl...
>>
>>I am going brain-dead today because I can't even recall how to get
>>the
>>HttpResponse returned from something like: http://www.whatismyip.org/
>>in
>>my code-behind or maybe in the Global.asax itself.
>>
>string strHostIP = String.Empty;
>using (WebClient objWebClient = new WebClient())
>{
> using (StreamReader objStreamReader = new
>StreamReader(objWebClient.OpenRead("http://www.whatismyip.org/")))
> {
> strHostIP = objStreamReader.ReadToEnd();
> objStreamReader.Close();
> }
>}
>>
>>
>--
>Mark Rae
>ASP.NET MVP
>http://www.markrae.net
>
>
>>
>>
>>

Mark Rae [MVP]
Guest
 
Posts: n/a
#15: Jul 15 '08

re: How can I get the IP address of the host Web Server into a Session variable?


"John Kotuby" <jkotuby75@comcast.netwrote in message
news:Oys0rId5IHA.4908@TK2MSFTNGP04.phx.gbl...
Quote:
Sure, why should anyone use VB when C# is far superior.
Indeed.
Quote:
The rest of the project is written in VB
Ah...
Quote:
I actually have not tried mixing C# and VB in the same project (primarily
because I am not too familiar with C#).
Not too difficult these days, in fact - involves little more than a couple
of subfolders under App_Code and some web.config entries...
Quote:
Also I guess my firewall is not set up properly because other posters have
said this should not work. I am not sure what they are driving at, but all
I see is a call out through port 80 which is usually left open at most
sites for Web surfing.
Correct.


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Closed Thread