Solved

UnknowHostException with Servlet

Posted on 2004-09-01
11
2,951 Views
Last Modified: 2013-11-24
Consider the following code (from javaalmanac, a bit modified):

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

public class URLTest
{
      public static void main(String [] arguments)
      {
      
            try
            {
                  System.out.println("setting properties...");
                  System.getProperties().put("http.proxySet", "true");
            System.getProperties().put("http.proxyHost", "192.168.6.92");
            System.getProperties().put("http.proxyPort", "8080");

                  // Create a URL for the desired page
                  URL url = new URL("http://www.yahoo.com");
                  System.out.println("URL object created...");
                  // Read all the text returned by the server
                  BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                  System.out.println("input from url obtained...");
                  String str;
                  while ((str = in.readLine()) != null)
                  {
                        System.out.println("line: " + str);
                  }
                  in.close();
            }
            catch (MalformedURLException e)
            {
                  System.out.println(e);
            }
            catch (IOException e)
            {
                  System.out.println(e);
            }
      }
}

This runs fine as a standalone application, no problems at all. However when I try to run it from a servlet (I literaly copy/paste the try...catch block) I am getting an UnknownHostException in the following line:

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

obviously when it tries to open the stream.

Any thoughts on how to resolve it?

Specifications:

Linux RedHat 9.0
WebLogic Server 8.1
JDK1.4.1_05

0
Comment
Question by:girionis
  • 5
  • 3
  • 3
11 Comments
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
0
 
LVL 35

Author Comment

by:girionis
Comment Utility
I am still getting the same error message with the code in the above link:

java.net.UnknownHostException: www.yahoo.com
        at java.net.InetAddress.getAllByName0(InetAddress.java:1004)
        at java.net.InetAddress.getAllByName0(InetAddress.java:969)
        at java.net.InetAddress.getAllByName(InetAddress.java:963)
        at weblogic.net.http.HttpClient.openServer(HttpClient.java:263)
        at weblogic.net.http.HttpClient.openServer(HttpClient.java:327)
        at weblogic.net.http.HttpClient.<init>(HttpClient.java:128)
        at weblogic.net.http.HttpURLConnection.getHttpClient(HttpURLConnection.java:127)
        at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:329)
        at java.net.URL.openStream(URL.java:960)
        at smswebapp.SMSServlet.processRequest(SMSServlet.java:39)
        at smswebapp.SMSServlet.doGet(SMSServlet.java:19)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6350)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
        at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
        at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)


LIne 39 is this one:

BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream()));
0
 
LVL 35

Author Comment

by:girionis
Comment Utility
It does not even run as a JSP.
0
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
i have removed these lines from my servlet and it is working fine with me

//   System.getProperties().put("http.proxySet", "true");
//        System.getProperties().put("http.proxyHost", "192.168.6.92");
//        System.getProperties().put("http.proxyPort", "8080");

0
 
LVL 18

Accepted Solution

by:
armoghan earned 400 total points
Comment Utility
I am using Jboss with Windows Xp

Probably there is some problem your http settings
You need to check on that
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 35

Author Comment

by:girionis
Comment Utility
I am behind a firewall, I need to have those. But I have also tried it without those, still the same problems. What app server are you using? Maybe it has got something to do with WebLogic. I am going to try Tomcat and see if it is any different.
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
Is the proxy host set correctly?
0
 
LVL 20

Assisted Solution

by:Venabili
Venabili earned 100 total points
Comment Utility
Seems like it is actually but for some reason it is not recognised. Any luck with the tomcat?
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
0
 
LVL 35

Author Comment

by:girionis
Comment Utility
I am not using Eclipse but I am compiling my servlet from command prompt. I think it has got something to do with WLS settings. I am trying to get Tomcat run my servlet atm so I will post any updates soon.
0
 
LVL 35

Author Comment

by:girionis
Comment Utility
It works with Tomcat, weird. This means that there are some settings issues with WLS, maybe it does not recognize the proxy settings as Venabili said. I will ask a separate question to WebLogic category for this.

Thank you all for your help :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now