Posted by John_Moylan (jfp), 14 February 2003Morning all
any clues on how to get just the server response from a http request in a cgi script?
Looking at redirecting if the response is good ie: 200 or somewhere else if bad (404 etc)
Theres got to be an easy way!
Posted by admin (Graham Ellis), 14 February 2003Are you looking at one server from another? Do you have the LWP module loaded on your server? If so, you can use that. The example that I have here is a stand along program, but I've done the same thing wrapped in a CGI script to have various servers talking to each other.
Response from http://www.wellho.net/index.html was code 200
Response from http://www.wellho.net/greatsite.html was code 404
Posted by admin (Graham Ellis), 15 February 2003Came back this morning and actually read your question
If you replace the GET with HEAD in my code, your script will read back just the header from the server and not the body ... then you'll be able to make your decision whether or not the server has good information at the present time
Posted by John_Moylan (jfp), 16 February 2003Thanks Graham, thats great.
Are you aware of a problem with useragent though? it throws a 500 error when the request is valid.
What I'm trying to do is get the server repsonse from a password protected page, so the url looks like:
Though it works in the browser the script using useagent throws a 500 eror? strange!
Have you encountered this?
Posted by admin (Graham Ellis), 17 February 2003Not a form of URL that I use personally ... but I'm very suprised at the error 500 as it usually indicates a server-side problem and not a fault in the request. However - I would have a detailed look at the request.
I've stepped back into some older examples - here is an "echo server" that takes whatever is thrown at it on port 8080 and echos it back as a web page. If you run this against your user agent script, and again using your regular browser, you might spot what's different in the requests and so be lead towards a solution.
Posted by John_Moylan (jfp), 18 February 2003Never did get to the bottom of this!
Ended up doing a 'wget -S' and regexing the response.
This was being done as a quick mockup in Perl, the real thing needs to be written in Java.
Is there anything similar to useragent or wget that would give the server response in Java?
Should I be cross posting this to the Java Forum?
Posted by John_Moylan (jfp), 18 February 2003Sorry, sorry sorry.
I'm breaking my own rules by posting without looking for myself.
A quick look at javadoc points me towards:
HttpURLConnection which has a method called getResponseCode()
Erm, that'll teach me, the answers always out there...and its normally in the docs, regardless of the language you use.
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho