I just realized I run a proxy server for http request on my firewall machine.  Maybe that's the problem.
www.yahoo.com probably is a special case.
Thank you so much for trying it out and your reply.  at least I know there is nothing wrong with the code.

Regards,

Jie

Tony LaPaso wrote:
You should not post to this group -- it's off topic, unrelated to
Servlets.
Sorry,  the code is part of my servlet code using Tomcat 4.0.  So, I automatically posted to this group.




I ran your code (after including necessary variable declarations)
on JDK 1.4.1 beta and it worked fine.

Here's the code I ran which worked....I don't know why it would
not work otherwise. Maybe your filewall is catching it...The Java
program may be setting different HTTP headers than your browser.
The firewall may be seeing those non-browser headers and blocking
your request. The probelm is weird though, since you say you can
reach Yahoo just fine.

try JDK 1.4.1 beta.



import java.io.*;
import java.net.*;

public class Tester {
public static void main(String[] args) {
String myUrl ="http://www.weather.com/outlook/travel/local/"
+ "94025" + "?";
try{
URL url = new URL(myUrl);

System.out.println("new URL");

HttpURLConnection urlConnection =
(HttpURLCon nection)url.openConnection();

System.out.println("openConnection");

int httpResponse = urlConnection.getResponseCode();

System.out.println("getResponse");

if (httpResponse != HttpURLConnection.HTTP_OK)
System.out.println("Error connecting to" + myUrl + "
Response Code = " + httpResponse);

InputStream inStream = urlConnection.getInputStream();

System.out.println("getInputStream");

BufferedReader bReader = new BufferedReader(new
InputStreamReader(inStream));
String line;
StringBuffer htmlStrBuffer = new StringBuffer();

while ((line = bReader.readLine()) != null) {
htmlStrBuffer.append(line);
htmlStrBuffer.append('\n');
}

System.out.println(htmlStrBuffer);

} catch(Exception e) {
e.printStackTrace();
}
} // main()
} // Tester









- ---- Original Message -----
From: "Jie Zhang" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, August 09, 2002 2:38 PM
Subject: HttpURLConnection


    Hi,

I am trying to use HttpURLConnection to grab a web page. In t
he
following code, if I change myUrl to "http://www.yahoo.com",
the code
runs fine.  But with the weather.com url,
urlConnection.getResponseCode() just
hang there forever. If I launch
"http://www.weather.com/outlook/travel/local/94025?" on any
browser, it
reaches the website fine.   Did anybody has similar experience
or any
suggestions?  Have I missed anything in the code ?

String myUrl ="http://www.weather.com/outlook/travel/local/" +
"94025" + "?"
    try{
URL url = new URL(myUrl);
System.out.println("new URL");
urlConnection =
(HttpURLConnection)url.openConnection();
        System.out.println("openConnection");
httpResponse = urlConnection.getResponseCode();
System.out.println("getResponse");
if (httpResponse != HttpURLConnection.HTTP_OK){
System.out.println("Error connecting to" +
myUrl+"
Response Code = "
+ String.valueOf(httpResponse));
}
inStream = urlConnection.getInputStream();
System.out.println("getInputStream");
System.out.println(myUrl);
BufferedReader bReader = new BufferedReader(new
InputStreamReader(inStream));
while ((line = bReader.readLine() ) != null) {
htmlStrBuffer.append(line);
htmlStrBuffer.append('\n');
}

}
catch(Exception e)
{
e.printStackTrace();
}

Thanks in advance,

Jie


_________________________________________________________________
__________
To unsubscribe, send email to [EMAIL PROTECTED] and include
in the body
of the message "signoff SERVLET-INTEREST".

Archives:
http://archives.java.sun.com/archives/servlet-interest.html
Resources:
http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html






Reply via email to