Tag Archives: web-services

Basics of HTTP

This is a refresher about the HTTP protocol (citation: RESTful Web Services, 2007, L.Richardson, S. Ruby, O’Rielly Media Inc).

What is HTTP?

HTTP stands for Hypertext Transfer Protocol.

Using the analogy of a document and envelope…

HTTP is a document based protocol:

  1. A client puts a document into an envelope and sends to a server.
  2. The sever puts a response envelope into an envelope and sends to the client.

HTTP has strict standards about the type of envelope but not what goes inside it.

HTTP Request (Client)

The HTTP Method

Can be GET, POST, DELETE, PUT, OPTIONS, HEAD, PROFIND, COPY, MOVE

The Path

The URI to the right of the hostname – this becomes the address on the envelope.

Request Headers

These are key-value pairs that specify infomation about the envelope – there are 46 in all – some of them include: Host, User-Agent, Accept, Keep-Alive…

The Entity Body/Document/Representation

The document inside the envelope.  GET requests never have an entity body, all the information necessary is included in the path and request header.

In POST requests – the entity body could be structured XML to pass over a substantial amount of complex data.  Some web services may take this data and make objects from the XML and feed it into a database, however you do not necesarily have to use XML you can just use plain text, you also don’t have to put the data in a database – you can do what you want with data once you recieve it on the database.

The following shows an example of a GET reqeust to the BBC:

(Request-Line)    GET / HTTP/1.1
Host    www.bbc.co.uk
User-Agent    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11
Accept    text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language    en-gb,en;q=0.5
Accept-Encoding    gzip,deflate
Accept-Charset    ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive    300
Proxy-Connection    keep-alive
Cookie    AMOS_PREF=sac%3Dg4; BBC-UID=e4c73e81d4fc55653460b7caa03036f31b6ba10990a0b114b4ffb881cf0ba6450Mozilla%2f5%2e0%20%28Windows%3b%20U%3b%20Windows%20NT%205%2e1%3b%20en%2dGB%3b%20rv%3a1%2e8%2e1%2e12%29%20Gecko%2f20080201%20Firefox%2f2%2e0%2e0%2e12; BBCNewsAudience=Domestic; BBCNewsAudcWght=-99; BBCMediaSelector=m%3Arm&b%3Abb&st%3Ah&ms3%3A4; hp=+acv+ba+neaj+hj+oab*+c1+g1ab+mc2+rad*+da+f1a7b7c7d7+i+kca+la

HTTP Response (Server)

HTTP Response Codes

The code tells the client what happened to their request – did it fail or was success achieved.   There are lots of codes, the most common you will have seen on the web is 404 – which means “Not Found” i.e. the web page was not found on the server.  A success code will usually be 200 i.e. if you got to see the web page you asked for.

Response Header

Pretty much the same as Request Headers e.g. Date, Server, Etag, Content-Type, Last-Modified…

The Entity Body/Document/Representation

In GET requests you actually get an entity body sent to you (or at least you should do!)   The Response Header also makes an important contribution here, like Content-Type – this will tell the client about the media-type to expect (there are many of these) but in the case of a browser rendering an HTML web page it will commonly be text/html.

N.B – media-type can also be referred to as MIME type, content type or data type.

The following is an example of a response from the BBC:

(Status-Line)    HTTP/1.1 200 OK
Via    1.1 BLADEWIN17
Connection    Keep-Alive
Proxy-Connection    Keep-Alive
Content-Length    110652
Date    Wed, 24 Jun 2009 16:00:01 GMT
Age    87
Content-Type    text/html
Server    Apache
Accept-Ranges    bytes
Keep-Alive    timeout=4, max=194

Advertisements

Beginners resources for REST web service with Eclipse

Just thought I’d quickly mention some good resources for learning the REST paradigm (having now moved on from SOAP web services.  These are basically resources I wish I had found on day 1.

The following is bearing in mind you are using the Restlet framework.

Again, a total lack of any decent, comprehensible tutorial material on the net, but I did unearth a gem from IBM’s website (you do have to register to download the doc, but it is free):

Build a RESTful Web service – An introduction to REST and the Restlet framework by Andrew Glover.

Also to back up your knowledge and to help you understand the above tutorial, I would suggest reading:

RESTful Web Services by Leornard Richardson and Sam Ruby from O’Reilly.

Enjoy!

PS – Hope to publish my findings on a making my first RESTful web service that writes into a database.

A Simple SOAP Web Service Example in Eclipse (Ganymede)

Huh?  Not C#!!! – What’s all this “Rant in C Sharp” business then!?  Well, sometimes you have to be flexible in your job – well, lets just say we should be ‘language ambivalent’.

This is a VERY simple attempt to show show how to create a web service in Eclipse using Axis.  There is also some client code to show how you would interact with that web service.  For some reason I had to delve into a bazillion websites and scour a million books before I could make some sort of sense of it all – so just so you don’t have the same trouble – I have decided to publish it (well actually, it’s so that I don’t forget either).

Little disclaimer – I’m not saying that this is difinitive, I may be missing something, but it seems to work for me just now, and I haven’t gone into all the ‘bells and whistles’ of web services.  At the moment I’m writing down everything I learn and hopefully help others by sharing the knowledge.

(Using Java 1.5)

Create Basic Web Service

  1. Create new Dynamic Web Project.
  2. Create new Java class (call it “DoesMagic”).
  3. Add method like this (perhaps):
public class DoesMagic{
    public String doBasicStuff(String message)
    {
        return "This is your message: " + message;
    }
}

4. Right click on class you just made, New -> Other -> Web Services -> Web Service, you should get a form like the one below:

This will create all the necessary files to be able to publish your class as a web service.  If you want you can also create a client automatically by moving the slider – but what it generates may be hard to understand at first glance, so I have written a simple example of client code (see later on…).

5.  In order to run the web service – just,  Run as -> Run on Server.  Once the web service is running and alive on Tomcat – then we should be able to use it via some client code (see next bit).

Create Basic Web Service Client

Must create a project that has all the axis.jar type stuff in the WebContent folder – this can usually be made by generating a client application via the above wizard – you don’t have to use all the auto generated classes to access your web service, just do the following:

1.  Create a new Java Class – call it TestClient.

2.  Make it a main class, and enter the following code:


import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class TestClient {

public static void main(String [] args)
{
    try{
       String endpoint = "http://localhost:8080/TestWebService/services/DoesMagic";

       Service service = new Service();
       Call call = (Call) service.createCall();
       call.setTargetEndpointAddress( new java.net.URL(endpoint) );
       call.setOperationName( new QName("http://testPackage.fc.com", "doBasicStuff") );

       String ret = (String) call.invoke( new Object[] {"Hello World"} );
       System.out.println(ret);
       }
       catch(Exception e){
           System.err.println(e.toString());
       }
}
}

3.  Go to your web service project you made before and look at the source code for the wsdl file that has been created.  It’s in the folder WebContent -> wsdl.  Inside there you will find a wsdl that is the same name as your class.

4. You need to look at the wsdl and find the endpoint – which looks similar to:

"http://localhost:8080/TestWebService/services/DoesMagic"

and you need to get the namespace and the method name you want to invoke  (as shown above, should be in a very similar format).

5. In this case, when we get to the point that the call.invoke command is being issued it is casting the result to a String and we are sending in a String called “Hello World” – this should create a message in the console like “This is your message: Hello World”.

6. To test the class, just run the project as a Java Application and you should see the result in the Console printed out.

Conclusion

This is not indepth – this may not even be 100% correct, but it just gives you a taste of what can be achieved – it’s up to you now to read up more on Java Web Services.  Good luck !

PS – Take a sec just to vote whether this article helped you or not – see widget below.  If not, tell me what’t wrong with it and I’ll try and improve it.  I love feedback so I look forward to your comments!