Web Services and Sockets

Web Services
  • Web service is a software system that provides a programmatic interface over HTTP.
  • In case of an XML it can also be called as an application to application interaction over TCP.
  • There are different ways of implementing a web service
    • SOAP Protocol 
      • XML data is placed inside a SOAP message and is transported using HTTP
      • Also called as SOAP based web services.
      • Place data in XML documents and transfer them directly using HTTP.This approach is commonly referred to as REST based web services.
      • REST stands for Representational State Transfer.
HTTP Request Header
  • The header is comprised of  a request line followed by 1 or more header lines.
  • The content length which is the number of bytes in the body of a request message.
    • "Content-length: " + docBytes.length + "\r\n"
  • Content Type
    • Specifies the MIME type of the object being transported in the body of request message.
    • "Content-type: text/xml\r\n"
  • Host header that indicates the host name we are sending message to.
    • Host: localhost\r\n
  • We send a connection header indicating that the underlying TCP connection will be closed by the client after the server returns a response.
    • "Connection: close\r\n"
  • The request line is the first line sent
    • It starts with an HTTP method, followed by the name of the resource on the server, followed by the version of the HTTP protocol being used.
    • "POST /newsfeed/publish HTTP/1.1\r\n"
Reading from Socket
  • We get input stream from socket.
    • This provides us with raw bytes being returned to use through the TCP connection represented by the socket.
  • We wrap the inputStream in an inputStreamReader
    • This converts the input stream of bytes into a stream of characters.
  • We wrap the inputStreamReader in a bufferedReader which allows us to read a line at a time.
           BufferedReader br = req.getReader();  
           SAXBuilder builder = new SAXBuilder();  
           Document requestDocument = null;  
           try  
           {  
                requestDocument = builder.build(br);  
           }  
           catch(JDOMException e)  
           {  
                throw new RuntimeException(e);  
           }  
           // Extract title and link from request.  
           Element item = requestDocument.getRootElement();  
           Element titleElement = item.getChild("title");  
           String title = titleElement.getText();  
           Element linkElement = item.getChild("link");  
           String link = linkElement.getText();  

s

No comments:

Post a Comment