473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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.
Jul 11 '08 #1
14 4021
"John Kotuby" <jk*******@comcast.netwrote in message
news:%2****************@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

Jul 11 '08 #2
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]" <ma**@markNOSPAMrae.netwrote in message
news:ec**************@TK2MSFTNGP06.phx.gbl...
"John Kotuby" <jk*******@comcast.netwrote in message
news:%2****************@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

Jul 11 '08 #3
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:
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]" <ma**@markNOSPAMrae.netwrote in message
news:ec**************@TK2MSFTNGP06.phx.gbl...
"John Kotuby" <jk*******@comcast.netwrote in message
news:%2****************@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


Jul 11 '08 #4
"John Kotuby" <jk*******@comcast.netwrote in message
news:eV**************@TK2MSFTNGP03.phx.gbl...
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

Jul 11 '08 #5
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]" <ma**@markNOSPAMrae.netwrote in message
news:uq**************@TK2MSFTNGP03.phx.gbl...
"John Kotuby" <jk*******@comcast.netwrote in message
news:eV**************@TK2MSFTNGP03.phx.gbl...
>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

Jul 11 '08 #6
"John Kotuby" <jk*******@comcast.netwrote in message
news:OL***************@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

Jul 11 '08 #7
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]" <ma**@markNOSPAMrae.netwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
"John Kotuby" <jk*******@comcast.netwrote in message
news:OL***************@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

Jul 11 '08 #8
"John Kotuby" <jk*******@comcast.netwrote in message
news:el**************@TK2MSFTNGP02.phx.gbl...

[top-posting corrected as usual]
I converted your code example to VB
Why on earth did you do that...?
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

Jul 11 '08 #9
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" <jk*******@comcast.netwrote in message
news:el**************@TK2MSFTNGP02.phx.gbl...
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]" <ma**@markNOSPAMrae.netwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
>"John Kotuby" <jk*******@comcast.netwrote in message
news:OL***************@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


Jul 11 '08 #10
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:
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" <jk*******@comcast.netwrote in message
news:el**************@TK2MSFTNGP02.phx.gbl...
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]" <ma**@markNOSPAMrae.netwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
"John Kotuby" <jk*******@comcast.netwrote in message
news:OL***************@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


Jul 12 '08 #11
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]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"John Kotuby" <jk*******@comcast.netwrote in message
news:el**************@TK2MSFTNGP02.phx.gbl...

[top-posting corrected as usual]
>I converted your code example to VB

Why on earth did you do that...?
>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

Jul 14 '08 #12
Thanks Jamal, that seems to work also.

"Jamal" <®ivodotnet®i@®iyahoo.comwrote in message
news:48**********************@cv.net...
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" <jk*******@comcast.netwrote in message
news:el**************@TK2MSFTNGP02.phx.gbl...
>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]" <ma**@markNOSPAMrae.netwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
>>"John Kotuby" <jk*******@comcast.netwrote in message
news:OL***************@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



Jul 14 '08 #13
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" <br*********@discussions.microsoft.comwrote in message
news:76**********************************@microsof t.com...
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:
>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" <jk*******@comcast.netwrote in message
news:el**************@TK2MSFTNGP02.phx.gbl...
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]" <ma**@markNOSPAMrae.netwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
"John Kotuby" <jk*******@comcast.netwrote in message
news:OL***************@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




Jul 14 '08 #14
"John Kotuby" <jk*******@comcast.netwrote in message
news:Oy**************@TK2MSFTNGP04.phx.gbl...
Sure, why should anyone use VB when C# is far superior.
Indeed.
The rest of the project is written in VB
Ah...
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...
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

Jul 14 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option...
5
by: Scott | last post by:
I'm trying to set a session variable after displaying a form, then capture the Session variable on postback. For some reason, the below code always returns the form, not the "Step 2" results part....
6
by: Al Jones | last post by:
This is a repost form the vbscript newgroup - if this isn't the appropriate group would you point me toward one that is. Basically, I seem to be losing session data part way though preparing an...
4
by: Stephen | last post by:
I have a .NET (1.1 framework) application that is losing a session variable on only a few PC's. The main page is loading up in a frame in a Portal application. On the Page_Load it stores an...
4
by: MrBiggles | last post by:
Here's the sitch: I read in a csv file with 60000 lines (20 fields per record), and store the data to a local array. The file is read in and stored just fine and pretty quick. Now, if I try to...
4
by: Sarah Marriott | last post by:
Our website contains session variables that are used to validate if a user is logged in etc. We have found that these variables are randomly lost while navigating the website. We set up some...
6
by: laredotornado | last post by:
Hi, When a user logs into our site, we create a session variable to denote the session is active and another to denote who is logged in. Once the user closes the browser and re-opens it, the...
8
by: YYZ | last post by:
I'm using asp, not asp.net. I've got some open ended questions that I was really hoping someone in here could answer, or direct me to some resources that will help me answer them on my own. ...
5
by: Twayne | last post by:
Hi, If ever a newbie wants to know how much he has to learn yet, he only has to look here<g>!! ANYway: PHP 5.2.5; XP Pro SP2+, local Apache Server My actual question is: How do I get a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.