Solved

how to get http status code

Posted on 2004-05-03
16
415 Views
Last Modified: 2012-06-21
Hi! Does anybody here know how to get http status code using ASP?  I tried running the code from http://www.asp101.com/samples/http.asp
but I didn't see any result. It didn't even said that there's an error.

Can someone please help me? Thanks!
0
Comment
Question by:lian_al
  • 7
  • 4
  • 2
16 Comments
 
LVL 21

Expert Comment

by:ap_sajith
ID: 10975810
Try this sample...

<%
  'Declare the variables, and set the value of the variable url as
  'the full URL of the requested page.
  Dim xml, strData, url
  url = "http://www.someserver.com/somepage.asp"

  'Next, create an instance of the MS XMLhttp component.
  'old version, unstable server side performance so upgrade if you can
  ' *** OLD *** Set xml = Server.CreateObject("Microsoft.XMLHTTP")
      
  'new version, better server side performance
  Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")

  'Now open the connection, and send the request to the remote server. You
  'need to set the optional Async parameter to True on the open method for
  'this script to work. Otherwise, the waitForResponse method used
  'below will have no effect.
  xml.Open "GET", url, true   ' the True specifies an asynchronous request
  Call xml.Send()

 'Turn off error handling
  On Error Resume Next
 
  'Wait for up to 3 seconds if we've not gotten the data yet
  If xml.readyState <> 4 then
    xml.waitForResponse 3
  End If

  'Did an error occur?  If so, use a default value for our data
  If Err.Number <> 0 then
    strData = "some default text..."
  Else
    If (xml.readyState <> 4) Or (xml.Status <> 200) Then
      'Abort the XMLHttp request
      xml.Abort
           
        strData = "Problem communicating with remote server..."
    Else
      strData = xml.ResponseText
    End If
  End If
%>

Cheers!!
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 10975842
as for the ASP 101 sample, are you just getting a blank screen?. Make sure that you are Passing the correct URL.

Cheers!!
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 10975921
Here is a sample working code...

<%
  'Declare the variables, and set the value of the variable url as
  'the full URL of the requested page.
  Dim xml, strData, url
  url = "http://localhost/test.asp"

  'Next, create an instance of the MS XMLhttp component.
  'old version, unstable server side performance so upgrade if you can
  ' *** OLD *** Set xml = Server.CreateObject("Microsoft.XMLHTTP")
     
  'new version, better server side performance
  Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")

  'Now open the connection, and send the request to the remote server. You
  'need to set the optional Async parameter to True on the open method for
  'this script to work. Otherwise, the waitForResponse method used
  'below will have no effect.
  xml.Open "GET", url, true   ' the True specifies an asynchronous request
  Call xml.Send()

 'Turn off error handling
  On Error Resume Next
 
  'Wait for up to 3 seconds if we've not gotten the data yet
  If xml.readyState <> 4 then
    xml.waitForResponse 3
  End If

  'Did an error occur?  If so, use a default value for our data
  If Err.Number <> 0 then
    strData = "some default text..."
  Else
    If (xml.readyState <> 4) Then
      'Abort the XMLHttp request
      xml.Abort
           
       strData = "Problem communicating with remote server..."
    Else
      strData = xml.Status & "<br><br><br>" & xml.ResponseText
    End If
  End If

  Response.write strData
%>

Change the URL parameter to the page you are trying to access. Also, make sure that the code works for pages within your network before trying pages on the internaet. The XMLHTTP access to the internet from the server could be blocked.

Cheers!!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:lian_al
ID: 10975961
I tried the code you gave me but I still got a blank screen. Do I have to install a component first or something? I'm using Win2000 Service Pack 3. I've already downloaded and installed MsXML4 sp2 from Microsoft.

Thanks ap_sajith!
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 10976598
Are you able to run other asp pages?. It wouldnt be a problem with the component not being installed because it should have thrown an error. Try removing the On error resume next statement.

Also, try a step by step analysis..

eg:

<%
  'Declare the variables, and set the value of the variable url as
  'the full URL of the requested page.
  Dim xml, strData, url
  url = "http://localhost/test.asp"

  'Next, create an instance of the MS XMLhttp component.
  'old version, unstable server side performance so upgrade if you can
  ' *** OLD *** Set xml = Server.CreateObject("Microsoft.XMLHTTP")
     
  'new version, better server side performance
  Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
If isIbject(Xml) Then
Response.write "Object Created"
Response.end
End If

etc.

Post the exact same code of the page you were trying to execute.

Cheers!!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10977691
>>I tried the code you gave me but I still got a blank screen. <<
That is because it is XML.  Try doing View Source and you will see it there.
0
 

Author Comment

by:lian_al
ID: 10982901
I tried the code you gave me..the object was created. But I still can't get it to display the status. I even tried viewing the source, but the status code could not be found there also.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10983107
>>I even tried viewing the source<<
Right the source should have shown you just the XML.

>>but the status code could not be found there also.<<
You can use:
Response.Write xml.Status

After the using the Send method.
0
 

Author Comment

by:lian_al
ID: 10983156
This is my code..

<%
  Dim xml, strData, url
  url = "http://www.google.com"

  Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
  xml.Open "GET", url, false   ' the True specifies an asynchronous request
  Call xml.Send()

  response.write xml.status

%>

When I viewed the source, here's the output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"></HEAD>
<BODY></BODY></HTML>

How come the status still isn't displayed? What else should I do? Thanks!!!
0
 

Author Comment

by:lian_al
ID: 10983862
Are there other ways on how I can get the status code? I've been searching through the Internet for days now..and upto now I still don't have any solution
0
 
LVL 21

Accepted Solution

by:
ap_sajith earned 125 total points
ID: 10985114
Create a new page called test.asp in the same folder you are running this asp page from. . Add the following code in the page.

<%=Now%>

Now change your code in the page to check status to..

<%
  Dim xml, strData, url
  url = "http://localhost/test.asp"
' ** You Can type in the actual URL if it is different from the one specified above.
' ** WE CAN TRY CONNECTING TO PAGES IN THE INTERNET LATER **

  Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
  xml.Open "GET", url, false   ' the True specifies an asynchronous request
  Call xml.Send()
  response.write xml.status
%>

Cheers!!
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 11020824
Any updates?

Cheers!!
0
 
LVL 21

Expert Comment

by:ap_sajith
ID: 11179292
Hi,
Any Updates?. Do you need any further assistance with this?. Please close this question if no further assistance is needed.
If you need help closing this question, please refer to http://www.experts-exchange.com/help.jsp#hs5 on how to close a question.

Cheers!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question