Solved

how to get http status code

Posted on 2004-05-03
16
408 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating TABS in ASP Classic NOT using DIV 22 67
Server Timeout with Loop 6 45
Error in query expression 3 42
Save ms data to server side. 19 42
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…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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