DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2005
    Posts
    1

    How to make http connection using java

    Hi,

    I have a Tomcat server running at port 8080 (thru jbuilder). I have written the following client code:


    BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
    message = messageText.getText();

    URL u = new URL ( "http://localhost:8080/Servlet1?Message=" + message );
    HttpURLConnection con = (HttpURLConnection) u.openConnection ();
    con.setDoInput(true);
    con.setRequestMethod("GET");
    con.connect();
    con.getResponseMessage();
    javax.swing.JOptionPane.showMessageDialog ( null, "Message Sent" );
    con.disconnect ();

    Basically, the server just has to read the parameter 'message' and store it in a database. When i type this exact url in the browser, it works. But when i run the above code in an application of my own, I get the following exception:


    java.io.FileNotFoundException <followed by the complete url>

    However, this message doesn't come if i remove the 'con.getResponseMessage();' line from the code. And whether or not this line is there, nothing gets written on the database!!!!

    if the url works from the browser, how come i get this message. Please tell me how can i make my application work?

  2. #2
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    Off hand I don't see you actually making the request. Use a known good URL like http://google.com and see if you do any better. Also check the server's access log and see if you're really talking to it.

  3. #3
    Join Date
    Jun 2005
    Posts
    3
    Try using like this

    URL u = new URL ( "http://localhost:8080/Servlet1?Message=" + message );
    URLConnection urlCon = servletUrl.openConnection();
    urlCon.setDoInput(true);
    con.setRequestMethod("GET");
    ObjectInputStream in = new ObjectInputStream(urlCon.getInputStream());
    in.close();
    urlCon = null;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links