Solved

how to get http status code

Posted on 2004-05-03
16
423 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 100
ASP Button to clear text 4 67
document.getElementById not worj with 2 IDs with the same name 10 57
Reg Exp to extract Url from string asp 12 35
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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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