Hello, I have getResponseCode and connect() prob here... I want to
connect then get the response code of the remote server. It's been
almost 12 hours but I havent been connected and the server has not
given me a response... I set the time out but it seems not to work..
(Time out is set to 10 seconds.) Here's my code:
System.setProperty( "sun.net.client.defaultReadTimeout",
Integer.toString( 10000 ) );
URL url = new URL(urlString);
URLConnection uc = url.openConnection();
HttpURLConnection hc = ((HttpURLConnection)uc);
hc.setRequestProperty("Accept","image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/x-shockwave-flash, */*");
.
.
.
uc.setRequestProperty("Content-Type","xml/html; charset=utf-8");
hc.setInstanceFollowRedirects(false);
hc.connect();
// -- Check if the response code.
if (HttpURLConnection.HTTP_OK == hc.getResponseCode() ||
HttpURLConnection.HTTP_MOVED_TEMP == hc.getResponseCode() )
{
System.out.println("(Success) Responce Code: "+hc.getResponseCode());
}
else
{
System.out.println("Responce Code: "+hc.getResponseCode());
}
// -- get headers
for (int counter=0; ; counter++)
{
String name = uc.getHeaderFieldKey(counter);
String value = uc.getHeaderField(counter);
System.out.println(name+": "+value);
}
If u think that there's nothing wrong with the connect and
getResponseCode, please help me with the time out thing. Thank you...
Manish Pandit - 08 Nov 2006 09:52 GMT
> // -- get headers
> for (int counter=0; ; counter++)
[quoted text clipped - 3 lines]
> System.out.println(name+": "+value);
> }
This to me looks like an infinite loop. Use uc.getHeaderFields() to get
a map, and iterate over the keys instead of a for loop like you have. I
think your code is running fine, and for the last 12 hours it is
printing null on the log/console.
-cheers,
Manish
Graham - 08 Nov 2006 09:54 GMT
Hi,
what value of urlString are you using?
Is it definately in the form: "http://<hostname or IP>[:<port>]/<page
to request>"
Graham
Lead Senior Developer
Modern Security Solutions
http://www.modernsecuritysolutions.com
> Hello, I have getResponseCode and connect() prob here... I want to
> connect then get the response code of the remote server. It's been
[quoted text clipped - 40 lines]
> If u think that there's nothing wrong with the connect and
> getResponseCode, please help me with the time out thing. Thank you...
IveCal - 08 Nov 2006 13:05 GMT
SORRY.... in addition to the get headers code:
I forgot to place the codition if I found a null...
// -- get headers
for (int counter=0; ; counter++)
{
String name = uc.getHeaderFieldKey(counter);
String value = uc.getHeaderField(counter);
System.out.println(name+": "+value);
/************* I forgot this part here ******************/
if (headerName == null && headerValue == null) {
// No more headers
break;
}
/************* I forgot this part here ******************/
}
My prog has those coditions ALREADY but it still did not word....
Here is the urlString where my prog fails:
https://ww2.lufthansa.com/online/portal/lh/us
Graham - 08 Nov 2006 13:24 GMT
It works for me without any changes. Are you behind a proxy?
Here is the output from your program when I run it:
(Success) Responce Code: 302
null: HTTP/1.1 302 Found
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Via: HTTP/1.1 deehqws060lhax6 (IBM-PROXY-WTE)
Location:
https://ww2.lufthansa.com/online/portal/lh/us?ctest=50850107291
Date: Wed, 08 Nov 2006 13:20:22 GMT
Server: IBM_HTTP_Server/2.0.47.1-PK07831 Apache/2.0.47 (Unix) DAV/2
Set-Cookie: lufthansa-cookie-check=OK;Path=/
Cache-Control: no-cache="set-cookie,set-cookie2"
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Content-Length: 0
Keep-Alive: timeout=10, max=1000
Content-Type: text/html; charset=ISO-8859-1
Content-Language: en
Warning: 214 deehqws060lhax6:82 "Transmogrified" "Wed, 08 Nov 2006
13:20:22 GMT"
null: null
Graham
Lead Senior Developer
Modern Security Solutions
http://www.modernsecuritysolutions.com
Graham - 08 Nov 2006 13:25 GMT
It works for me without any changes. Are you behind a proxy?
Here is the output from your program when I run it:
(Success) Responce Code: 302
null: HTTP/1.1 302 Found
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Via: HTTP/1.1 deehqws060lhax6 (IBM-PROXY-WTE)
Location:
https://ww2.lufthansa.com/online/portal/lh/us?ctest=50850107291
Date: Wed, 08 Nov 2006 13:20:22 GMT
Server: IBM_HTTP_Server/2.0.47.1-PK07831 Apache/2.0.47 (Unix) DAV/2
Set-Cookie: lufthansa-cookie-check=OK;Path=/
Cache-Control: no-cache="set-cookie,set-cookie2"
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Content-Length: 0
Keep-Alive: timeout=10, max=1000
Content-Type: text/html; charset=ISO-8859-1
Content-Language: en
Warning: 214 deehqws060lhax6:82 "Transmogrified" "Wed, 08 Nov 2006
13:20:22 GMT"
null: null
Graham
Lead Senior Developer
Modern Security Solutions
http://www.modernsecuritysolutions.com