Tanto si utiliza una llamada estándar a un componente existente del servidor de aplicaciones como si añade un nuevo fragmento de código para que sea el homólogo en el servidor del asesor personalizado, posiblemente deseará examinar los valores devueltos y cambiar el comportamiento del servidor.
GET /index.html HTTP/1.0 90
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>
Si sabe con exactitud qué códigos puede devolver posiblemente el servidor, el código quizá no tenga que ser tan detallado como el de este ejemplo. No obstante, tenga en cuenta que la limitación de los códigos de retorno que detecte también puede limitar la futura flexibilidad del programa.
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());
}
}
}