473,725 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Head ers.Item("Host" ).ToString
strRootWeb = "http://" + strRootWeb
Session.Add("Ro otWeb", strRootWeb)

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

Public strRootWeb = Session("RootWe b")
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 4055
"John Kotuby" <jk*******@comc ast.netwrote in message
news:%2******** ********@TK2MSF TNGP06.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.ServerV ariables["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.Head ers.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**@markNOSPA Mrae.netwrote in message
news:ec******** ******@TK2MSFTN GP06.phx.gbl...
"John Kotuby" <jk*******@comc ast.netwrote in message
news:%2******** ********@TK2MSF TNGP06.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.ServerV ariables["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.Head ers.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**@markNOSPA Mrae.netwrote in message
news:ec******** ******@TK2MSFTN GP06.phx.gbl...
"John Kotuby" <jk*******@comc ast.netwrote in message
news:%2******** ********@TK2MSF TNGP06.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.ServerV ariables["LOCAL_ADDR "].ToString();
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


Jul 11 '08 #4
"John Kotuby" <jk*******@comc ast.netwrote in message
news:eV******** ******@TK2MSFTN GP03.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**@markNOSPA Mrae.netwrote in message
news:uq******** ******@TK2MSFTN GP03.phx.gbl...
"John Kotuby" <jk*******@comc ast.netwrote in message
news:eV******** ******@TK2MSFTN GP03.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*******@comc ast.netwrote in message
news:OL******** *******@TK2MSFT NGP05.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(ob jWebClient.Open Read("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**@markNOSPA Mrae.netwrote in message
news:em******** ******@TK2MSFTN GP02.phx.gbl...
"John Kotuby" <jk*******@comc ast.netwrote in message
news:OL******** *******@TK2MSFT NGP05.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(ob jWebClient.Open Read("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*******@comc ast.netwrote in message
news:el******** ******@TK2MSFTN GP02.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 MyIPaddressButt on_Click(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles MyIPaddressButt on.Click
Dim strHostIP As String = String.Empty

Using objWebClient As New WebClient()

Using objStreamReader As StreamReader = New
StreamReader(ob jWebClient.Open Read
("http://www.whatismyip. com/automation/n09230945.asp") )

strHostIP = objStreamReader .ReadToEnd()

objStreamReader .Close()

End Using

End Using

End Sub

Jamal

"John Kotuby" <jk*******@comc ast.netwrote in message
news:el******** ******@TK2MSFTN GP02.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**@markNOSPA Mrae.netwrote in message
news:em******** ******@TK2MSFTN GP02.phx.gbl...
>"John Kotuby" <jk*******@comc ast.netwrote in message
news:OL******* ********@TK2MSF TNGP05.phx.gbl. ..
>>I am going brain-dead today because I can't even recall how to get the
HttpRespons e 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(o bjWebClient.Ope nRead("http://www.whatismyip. org/")))
{
strHostIP = objStreamReader .ReadToEnd();
objStreamReader .Close();
}
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


Jul 11 '08 #10

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

Similar topics

11
3349
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 Explicit and Response.Expires=-1,
5
4797
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. What am I doing wrong? It's on a local network server, not on the web if that matters. <% '* Step 2: Display results '**********************************************
6
2389
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 email from (possibly) three seperate forms. the following code is the end of a routine which stashes data from the first form off to session variables and then redirects itself to the proper form / procedure depending upon the state of two...
4
2168
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 object with the user id and password into the session. The web page includes a set of links. When the user clicks on a link, another page within this application is opened in a new window. On the Page_Load of this second page, I am retrieving...
4
3963
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 assign that array to a session variable it chokes. e.g. create array and load each element with a row from the file (btw, each row is an array as well, using fgetcsv()). When local array is loaded, I assign to session var as so: $_SESSION =...
4
2867
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 basic code (as shown below) which used a meta tag to refresh an aspx page every second. It tests whether there is any content in a session variable, if not then it writes a timestamp to a text file, recreates the data in the variable and continues....
6
5675
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 session is destroyed and the variables are gone. How can I keep the session alive for 24 hours even if the user closes and re-opens the browser?
8
4197
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. First, the session object. When a new user comes to my site, asp creates a session object, and a session id on that object. That session id is sent back to the client and stored as a cookie(?) that can be used to identify a single user across...
5
2205
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 POST variable into a session variable so I can use it in any other page I want to use it in? Apparently posted variables are only available in the Form page in
0
8747
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9392
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9246
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9162
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8069
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6694
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3211
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2150
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.