Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to check if a URL exists or not

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Groovy script how can I check whether a URL exists or not?
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please define 'exists'
1) Well formed?
2) Returns 200 status?
3) Anything else?
 
pritishkanta sahu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Welcome to the Ranch!

Please define 'exists'
1) Well formed?
2) Returns 200 status?
3) Anything else?




Hello Maneesh,

Suppose you have a URL www.yahoo.com .When you type this you the site opens . But i want to check whether www. yahoo.com exists or not using groovy
 
Maneesh Godbole
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you wish to use #2 from my list above.If yes this might help
http://groovy.codehaus.org/modules/http-builder/doc/get.html
 
pritishkanta sahu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Maneesh for the help.

I am getting an exception when i am running the groovy script .
java.net.UnknownHostException: www.yahoo.com.
 
Maneesh Godbole
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

pritishkanta sahu wrote:
I am getting an exception when i am running the groovy script .
java.net.UnknownHostException: www.yahoo.com.


Thrown to indicate that the IP address of a host could not be determined.


Can you access it from your browser which runs on the same machine the code runs?
Did you try with http://wwww.yahoo.com ?
If neither of these work, maybe you can try with any URL which opens in the browser running on the same machine your code runs
 
pritishkanta sahu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i am able to run www.yahoo.com on the machine.

Below is the code what in am using

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT


class TestUrl {
static void main(def args)
{

def http = new HTTPBuilder()

def request = http.request( 'http://www.google.com', GET, TEXT ) { req ->
uri.path = '/search'
uri.query = [ v:'1.0', q: 'Calvin and Hobbes' ]
headers.'User-Agent' = "Mozilla/5.0 Firefox/3.0.4"
headers.Accept = 'application/json'

response.success = { resp, reader ->
assert resp.statusLine.statusCode == 200
println "Got response: ${resp.statusLine}"
println "Content-Type: ${resp.headers.'Content-Type'}"
println reader.text
}

response.'404' = {
println 'Not found'
}
}


}
}

 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try http://98.139.183.24 instead of http://yahoo.com to see what happens.

If you want to check if a url is good without opening it, investigate pinging.
 
pritishkanta sahu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the following exception in Eclipse on running the groovy script and i replace the www.google.com with ip address http://74.125.224.72/


Caught: org.apache.http.conn.HttpHostConnectException: Connection to http://74.125.224.72 refused
org.apache.http.conn.HttpHostConnectException: Connection to http://74.125.224.72 refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1066)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1044)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:506)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:425)
at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:401)
at groovyx.net.http.HTTPBuilder$request.call(Unknown Source)
at TestUrl.main(TestUrl.groovy:12)
Caused by: java.net.ConnectException: Connection timed out: connect
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
... 11 more.
I want to know the code what i have wriiten is correct for checking whether a URL exist or not.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic