*/

protected void connect!)

}

try

}

while (true)

{

// Соединение находится в состоянии «установка». conn = Connector.open(uri);

httpConn = (HttpConnection) conn;

httpConn.setRequestProperty("method", HttpConnection.HEAD);

httpConn.setRequestProperty("User-Agent", userAgentID());

// Соединение находится в состоянии «установлено». if (resourceRelocated())

{

uri = httpConn.getHeaderField("location");

// Соединение находится в состоянии «отключено» после

// вызова close().

conn.close();

}

else

}

breaX;

*/

if (serverError())

{

conn.close (); return;

}

// Соединение находится в состоянии «установлено», is = httpConn.openlnputStream ();

System.out.println("Input stream class name = " + is.getClassO.get Name ());

int responseCode = httpCcnn.getResponseCode ();

printResponseCode (responseCode); catch (lOExceptior. ioe)

{

contents.append(ioe.getMessage());

System.out.println(ioe.getMessage());

ioe.printStackTrace();

}

}

private boolean resourceRelocated()

{

boolean relocated = false; try

}

status = httpConn.getResponseCode();

if (status == HttpConnection.HTTP_MOVED_TEMP II

status == HttpConnection.HTTP_MOVED_PERM II

status == HttpConnection.HTTP_TEMP_REDIRECT)

{

relocated = true;

}

}

catch (lOException ioe)

}

System.out.println(ioe.getMessage());

ioe.printStackTrace();

}

return relocated;

}

private boolean serverError ()

{

boolean error = false;

try

{

status = httpConn.getResponseCode();

if ((status == HttpConnection.HTTP_NOT_IMPLEMENTED)

If (status == HttpConnection.HTTP_VERSION)

If (status == HttpConnection.HTTP_INTERNAL_ERROR)

If (status = = HttpConnection.HTTP_GATEWAY_TIMEOUT)

If (status == HttpConnection.HTTP_BAD_GATEWAY))

}

error = true; } }

catch (lOException ioe)

{

error = true;

System.out.println(ioe.getMessage());

ioe.printStackTrace();

}

return error;

}

private void parse()

(

if (httpConn == null) return;

String protocol = httpConn.getProtocol();

contents.append("Protocol: " t protocol + "\n");

String type = httpConn.getType();

content's. append ("Type: " + type + "\n");

String encoding = httpConn.getEncoding ();

contents.append("Encoding: " + encoding + "\n");

long length = httpConn.getLength ();

contents.append("Length: " + length + "\n");

String uri = httpConn.getURL();

contents.append("URL: " + uri + "\n");

String host = httpConn.getHost();

contents.append("Host: " + host + "\n");

String query = httpConn.getQuery();

contents.append("Query: " + query + "\n");

String requestMethod = httpConn.getRequestMethod();

contents.append ("Method: " + requestMethod + "\n");

}

private void printResponseCode(int code)

{

System.out.print("Response code:

**/

switch (code) case HttpConnection.HTTP_ACCEPTED:

Systern.out.print In("HTTP_ACCEPTED"); break;

case HttpConnection.HTTP_BAD_GATEWAY:

Systern.out.print In("HTTP_BAD_GATEWAY"); break;

case HttpConnection.HTTP_BAD_METHOD:

Systern.out.print In("HTTP_BAD_METHOD"); break;

'case HttpConnection.HTTP_BAD_REQUEST:

Systern.out.print In("HTTP~BAD_REQUEST"); break;

case HttpCo-.nection.HTTP_CONFLICT:

System.out.println("HTTP_CONFLICT"); break;

case HttpConnection.HTTP_CREATED:

System.out.print In("HTTP_CREATED"); break;

case HttpConnection.HTTP_FORBIDDEN:

System.out.print In("HTTP_BAD_FORBIDDEN"); break;

case HttpConnection.HTTP_GATEWAY_TIMEOUT:

System.out.print In("HTTP_GATEWAY_TIMEOUT"); break;

case HttpConnection.HTTP_GONE:

Systern.out.print In("HTTP_GONE"); break;

case HttpConnection.HTTP_NO_CONTENT:

System.out.println("HTTP_NO_CONTENT"); break;

case HttpConnection.HTTP_NOT_ACCEPTABLE:

Systern.out.print In("HTTP_NOT_ACCEPTABLE"); break;

case HttpConnection.HTTP_NOT_FOUND:

System.out.print In("HTTP~NOT_FOUND"); break;

case HttpConnection.HTTP_OK:

System.out.println("HTTP_OK"); break;

case HttpConnection.HTTP_PROXY_AUTH:

Systern.out.print In("HTTP_PROXY_AUTH"); break;

case HttpConnection.HTTP_UNAVAILABLE:

Systern.out.print In("HTTP_UNAVAILABLE"); break;

case HttpConnection.HTTP_VERSION:

System.out.print In("HTTP_VERSION"); break; default:

System.out.println ();;. }

/**

Перейти на страницу:

Похожие книги