DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Location
    Cape Town, SA
    Posts
    8

    Question Getting data from a web service from a java application

    Hi Experts.

    I have been working in Java for a while now, but have never had to interact with a web service. I have built a program using the JSDK that needs to query a web service. I'm not sure where to start with this .

    All i know is that I will be getting a URL that i must call and i must pass a number to this URL. I must then expect a XML document back.

    I'm maybe looking for someone to tell me more or less what to expect or do .

    1) How do I link to the web service from a Java application? Do i simply create a socket connection to the URL on port 80. Example http://www.dummywebservice.com?number=1000.

    2) Is there any other information (http header) that must be sent?

    3) Is the XML returned via the socket connection? Do i just read this in as if i was reading in a object over a socket?

    4) Are there any existing simple web services that follow i can use to query try and get this working? What are they and how do I query them?

    I'm really new to this and it kind of needs to be done quick. Any help would be really good. If possible demo code will help a lot.

    Please help me, Thanks.

    (MC3)RaVeN
    ___
    _/\/ \/\_
    (MC3)RaVeN

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I've posted this one many times....

    ..but noone has complained yet, so I guess it's working
    Code:
    import java.net.*;
    import java.io.*;
    /**
     * Gets the contents of an url as a stringBuffer.
      Usage example:
    try {
      URL url=new URL("http://www.whatever.com/servlets/xyz?number=1000");
      SiteConn aConnection=new SiteConn(url);
      StringBuffer sb=aConnection.getContens();
      System.out.println(sb.toString());
    } catch (MalFormedURLException mfe) {
      System.err.println(mfe.getMessage());
    }
    
     *
     */
    
    class SiteConn {
      URL url = null;
      public SiteConn(URL url) {
        this.url = url;
      }
    
      public StringBuffer getContents() throws Exception {
        StringBuffer buffer;
        String line;
        int responseCode;
        HttpURLConnection connection;
        InputStream input;
        BufferedReader dataInput;
        connection = (HttpURLConnection) url.openConnection();
        responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
          throw new Exception("HTTP response code: " +
                              String.valueOf(responseCode));
        }
        try {
          buffer = new StringBuffer();
          input = connection.getInputStream();
          dataInput = new BufferedReader(new InputStreamReader(input));
          while ( (line = dataInput.readLine()) != null) {
            buffer.append(line);
            buffer.append("\r\n");
          }
          input.close();
        }
        catch (Exception ex) {
          ex.printStackTrace(System.err);
          return null;
        }
        return buffer;
      }
    }
    Last edited by sjalle; 10-26-2005 at 05:48 AM. Reason: SORRY! I posted a lightweight version...
    eschew obfuscation

  3. #3
    Join Date
    Apr 2004
    Location
    Cape Town, SA
    Posts
    8
    Hi Sjalle.

    Very sorry you having to hear this one again. It is sort of what i thought, but the code really helps my understanding.
    Thanks a lot .

    (MC3)RaVeN.
    ___
    _/\/ \/\_
    (MC3)RaVeN

Similar Threads

  1. Replies: 0
    Last Post: 06-21-2005, 11:01 AM
  2. Document-style Web Service Development in JAVA
    By V. Ramasamy in forum Java
    Replies: 0
    Last Post: 04-22-2003, 10:07 PM
  3. Replies: 0
    Last Post: 03-20-2003, 04:20 AM
  4. Replies: 0
    Last Post: 04-30-2001, 04:00 PM
  5. using Web Service classes
    By Tim Romano in forum .NET
    Replies: 6
    Last Post: 04-19-2001, 02:51 PM

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