Esempio: Utilizzo dei dati restituiti dagli advisor

Sia che si utilizzi una chiamata standard a una parte esistente del server delle applicazioni che si aggiunga una nuova parte di codice alla controparte server del proprio advisor personalizzato, è possibile esaminare i valori di carico restituiti e modificare il comportamento del server.

La classe Java StringTokenizer e i relativi metodi associati facilitano questa analisi. Il contenuto di un tipico comando HTTP potrebbe essere
GET /index.html HTTP/1.0 90
Una risposta tipica a questo comando potrebbe essere:
HTTP/1.1 200 OK
Date: Mon, 20 November 2000 14:09:57 GMT
Server: Apache/1.3.12 (Linux and UNIX)
Content-Location: index.html.en
Vary: negotiate
TCN: choice
Last-Modified: Fri, 20 Oct 2000 15:58:35 GMT
ETag: "14f3e5-1a8-39f06bab;39f06a02"
Accept-Ranges: bytes
Content-Length: 424
Connection: close
Content-Type: text/html
Content-Language: en

<!DOCTYPE HTML PUBLIC "-//w3c//DTD HTML 3.2 Final//EN">
<HTML><HEAD><TITLE>Test Page</TITLE></HEAD>
<BODY><H1>Apache server</H1>
<HR>
<P><P>This Web server is running Apache 1.3.12.
</P>
<P><IMG SRC="apache_pb.gif" ALT="">
</P></P>
</HR>
</BODY></HTML>
Gli elementi di interesse si trovano sulla prima riga, ovvero il codice di ritorno HTTP. La specifica HTTP classifica i codici di ritorno che possono essere così riepilogati:

Se si conosce precisamente il tipo di codice che può essere restituito dal server, il codice può non essere dettagliato come in questo esempio. Tuttavia, tenere presente che limitando i codici di ritorno che possono essere rilevati si potrebbe limitare la flessibilità futura del programma.

Il seguente esempio è un programma Java autonomo che contiene un client HTTP minimo. L'esempio richiama un parser semplice, di scopo generale, per esaminare le risposte HTTP.
import java.io.*;
import java.util.*;
import java.net.*;

public class ParseTest {
   static final int iPort = 80;
   static final String sServer = "www.ibm.com";
   static final String sQuery = "GET /index.html HTTP/1.0\r\n\r\n";
   static final String sHTTP10 = "HTTP/1.0";
   static final String sHTTP11 = "HTTP/1.1";

   public static void main(String[] Arg) {
     String sHTTPVersion = null;
     String sHTTPReturnCode = null;
     String sResponse = null; int iRc = 0;
     BufferedReader brIn = null;
     PrintWriter psOut = null;
     Socket soServer= null;
     StringBuffer sbText = new
     StringBuffer(40);

   try {
     soServer = new Socket(sServer, iPort);
     brIn = new BufferedReader(new InputStreamReader(
                                   soServer.getInputStream()));
     psOut = new PrintWriter(soServer.getOutputStream());
     psOut.println(sQuery);
     psOut.flush();
     sResponse = brIn.readLine();
     try {
       soServer.close();
     } catch (Exception sc) {;}
   } catch (Exception swr) {;}

   StringTokenizer st = new StringTokenizer(sResponse, " ");
   if (true == st.hasMoreTokens()) {
     sHTTPVersion = st.nextToken();
     if (sHTTPVersion.equals(sHTTP110) || sHTTPVersion.equals(sHTTP11)) {
       System.out.println("HTTP Version: " + sHTTPVersion);
     } else { 
       System.out.println("Invalid HTTP Version: " + sHTTPVersion); 
     } 
   } else {
     System.out.println("Nothing was returned"); 
     return; 
   } 

   if (true == st.hasMoreTokens()) { 
     sHTTPReturnCode = st.nextToken(); 
     try { 
       iRc = Integer.parseInt(sHTTPReturnCode); 
     } catch (NumberFormatException ne) {;}

     switch (iRc) { 
     case(200):
       System.out.println("HTTP Response code: OK, " + iRc); 
       break; 
     case(400): case(401): case(402): case(403): case(404): 
       System.out.println("HTTP Response code: Client Error, " + iRc); 
       break; 
     case(500): case(501): case(502): case(503): 
       System.out.println("HTTP Response code: Server Error, " + iRc); 
       break; 
     default: 
       System.out.println("HTTP Response code: Unknown, " + iRc); 
       break; 
     } 
   }

   if (true == st.hasMoreTokens()) { 
     while (true == st.hasMoreTokens()) { 
       sbText.append(st.nextToken()); 
       sbText.append(" "); 
       } 
     System.out.println("HTTP Response phrase: " + sbText.toString()); 
   } 
  } 
}
Argomento di riferimento    

Clausole e condizioni per i centri informazioni | Feedback

Ultimo aggiornamento: May 14, 2012 01:50 PM EDT
Nome file: rprf_advexreturn.html