Http Test
/*
* @(#)HttpTest.java 1.14 00/05/24 Copyright (c) 1999,2000 Sun Microsystems,
* Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with the terms
* of the license agreement you entered into with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES.
*/
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
public class HttpTest extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.SCREEN, 2);
private Command reloadCommand = new Command("Reload", Command.SCREEN, 1);
private Command headCommand = new Command("Head", Command.HELP, 1);
private String getUrl = "http://sraju1:8080/hello.txt";
private String headUrl = "http://sraju1:8080/hello.txt?test#home";
private Display display;
public HttpTest() {
display = Display.getDisplay(this);
}
public void startApp() {
// Use the specified URL is overriden in the descriptor
String u = getAppProperty("HttpTest-Url");
if (u != null) {
getUrl = u;
headUrl = u + "?test#home";
}
readPage();
}
private void headPage() {
StringBuffer b = new StringBuffer();
try {
HttpConnection c = null;
c = (HttpConnection) Connector.open(headUrl);
// c.setRequestMethod(request);
c.setRequestMethod(HttpConnection.HEAD);
b.append("URL: " + c.getURL() + "\nProtocol: " + c.getProtocol()
+ "\nHost: " + c.getHost() + "\nFile: " + c.getFile()
+ "\nRef: " + c.getRef() + "\nQuery: " + c.getQuery()
+ "\nPort: " + c.getPort() + "\nMethod: "
+ c.getRequestMethod());
InputStream is = c.openInputStream();
// DEBUG: request System.out.println(b) ;
b.append("\nResponseCode: " + c.getResponseCode()
+ "\nResponseMessage:" + c.getResponseMessage()
+ "\nContentLength: " + c.getLength() + "\nContentType: "
+ c.getType() + "\nContentEncoding: " + c.getEncoding()
+ "\nContentExpiration: " + c.getExpiration() + "\nDate: "
+ c.getDate() + "\nLast-Modified: " + c.getLastModified()
+ "\n\n");
int h = 0;
while (true) {
try {
String key = c.getHeaderFieldKey(h);
if (key == null)
break;
String value = c.getHeaderField(h);
b.append(key + ": " + value + "\n");
h++;
System.out.println(key + "(" + h + "): " + value);
} catch (Exception e) {
System.out.println("Exception while fetching headers");
break;
}
}
try {
is.close();
c.close();
} catch (Exception ce) {
System.out.println("Error closing connection");
}
TextBox t = new TextBox("Http Test", b.toString(), 1024, 0);
System.out.println("String read from URL: " + getUrl + " - is: "
+ b.toString());
t.addCommand(reloadCommand);
t.addCommand(exitCommand);
t.setCommandListener(this);
display.setCurrent(t);
} catch (IOException ex) {
System.out.println("Exception reading from http:" + ex);
}
}
private void readPage() {
StringBuffer b = new StringBuffer();
HttpConnection c = null;
TextBox t = null;
try {
long len = 0;
int ch = 0;
System.out.println("Read Page: " + getUrl);
c = (HttpConnection) Connector.open(getUrl);
InputStream is = c.openInputStream();
len = c.getLength();
if (len != -1) {
// Read exactly Content-Length bytes
// DEBUG: System.out.println("Content-Length: " + len);
for (int i = 0; i < len; i++)
if ((ch = is.read()) != -1)
b.append((char) ch);
} else {
// Read til the connection is closed.
// (Typical HTTP/1.0 script generated output)
while ((ch = is.read()) != -1) {
// DEBUG: System.out.println("'" + (char)ch + "' " +
// is.available() );
len = is.available(); // For HTTP 1.1 servers, chunked
// reads.
b.append((char) ch);
}
}
try {
is.close();
c.close();
} catch (Exception ce) {
System.out.println("Error closing connection");
}
try {
len = is.available();
System.out
.println("Inputstream failed to throw IOException after close");
} catch (IOException io) {
// Test to make sure available() is only valid while
// the connection is still open.,
}
t = new TextBox("Http Test", b.toString(), 1024, 0);
} catch (IOException ex) {
System.out.println("Exception reading from http");
if (c != null) {
try {
String s = c.getResponseMessage();
System.out.println(s);
if (s == null)
s = "No Response message";
t = new TextBox("Http Error", s, 1024, 0);
} catch (IOException e) {
ex.printStackTrace();
String s = ex.toString();
System.out.println(s);
if (s == null)
s = ex.getClass().getName();
t = new TextBox("Http Error", s, 1024, 0);
}
} else {
t = new TextBox("Http Error", "Could not open URL", 1024, 0);
}
}
t.addCommand(reloadCommand);
t.addCommand(exitCommand);
t.addCommand(headCommand);
t.setCommandListener(this);
display.setCurrent(t);
}
/*
* private void readPage() {
*
* StringBuffer b = new StringBuffer();
*
* HttpConnection c = null; TextBox t = null; try { c = ( HttpConnection
* )Connector.open( getUrl ); InputStream is = c.openInputStream(); int ch =
* 0; while ( (ch = is.read() ) != -1 ) { b.append( ( char )ch ); }
* is.close(); c.close();
*
* t = new TextBox( "Http Test", b.toString(), 1024, 0 );
* System.out.println( "String read from URL: " + getUrl + " - is: " +
* b.toString() ); } catch ( IOException ex ) { System.out.println(
* "Exception reading from http" ); if (c != null) { try { String s =
* c.getResponseMessage(); System.out.println( s ); if ( s == null ) s = "No
* Response message"; t = new TextBox( "Http Error", s, 1024, 0 ); } catch (
* IOException e ) { String s = ex.toString(); System.out.println( s ); if (
* s == null ) s = ex.getClass().getName(); t = new TextBox( "Http Error",
* s, 1024, 0 ); } } else { t = new TextBox( "Http Error", "Could not open
* URL", 1024, 0 ); } } t.addCommand( reloadCommand ); t.addCommand(
* exitCommand ); t.addCommand( headCommand ); t.setCommandListener( this );
* display.setCurrent( t ); }
*/
/*
* private void headPage() {
*
* StringBuffer b = new StringBuffer(); HttpConnection c = null; InputStream
* is = null;
*
* try { c = ( HttpConnection )Connector.open( headUrl );
* System.out.println( "Going to setRequestMethod()... " );
* c.setRequestMethod( HttpConnection.HEAD ); System.out.println( "Going to
* get Properties " ); b.append ( "URL: " + c.getURL( ) + "\nProtocol: " +
* c.getProtocol() + "\nHost: " + c.getHost() + "\nFile: " + c.getFile() +
* "\nRef: " + c.getRef() + "\nQuery: " + c.getQuery() + "\nPort: " +
* c.getPort() + "\nMethod: " + c.getRequestMethod()) ;
*
* System.out.println( "Gonna openInputStream... " ); is =
* c.openInputStream();
*
* System.out.println( "Going to get Properties after OpeninputStream" );
* b.append( "\nResponseCode: " + c.getResponseCode() + "\nResponseMessage:" +
* c.getResponseMessage() + "\nContentLength: " + c.getLength() +
* "\nContentType: " + c.getType() + "\nContentEncoding: " + c.getEncoding() +
* "\nContentExpiration: " + c.getExpiration() + "\nDate: " + c.getDate() +
* "\nLast-Modified: " + c.getLastModified() + "\n\n" );
*
* System.out.println( "Done getting properties after OpenInputStream " );
* int h = 0 ; while ( true ) { try {
*
* System.out.println( "Gonna get HeaderFieldKey... " ); String key =
* c.getHeaderFieldKey( h ); System.out.println( "Gonna get HeaderField... " );
* System.out.println( h + ", HeaderFieldKey is: " + key ); String value =
* c.getHeaderField( h ); System.out.println( h + ", HeaderField is: " +
* value ); b.append ( key + ": " + value + "\n" ); h++ ; } catch (
* Exception e ) { e.printStackTrace(); break; } }
*
* TextBox t = new TextBox( "Http Test", b.toString(), 1024, 0 );
* System.out.println ( b.toString() );
*
* t.addCommand( reloadCommand ); t.addCommand( exitCommand );
* t.setCommandListener( this ); display.setCurrent( t );
* } catch ( IOException ex ) { System.out.println( "Exception reading from
* http" ); ex.printStackTrace(); } finally { try { if ( is != null ) {
* is.close(); } if ( c != null ) { c.close(); } } catch( Exception e ) {
* e.printStackTrace(); }
* } }
*/
/**
* Pause signals the thread to stop by clearing the thread field. If stopped
* before done with the iterations it will be restarted from scratch later.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything. The thread is signaled to stop and no
* result is produced.
*/
public void destroyApp(boolean unconditional) {
}
/*
* Respond to commands, including exit
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
} else if (c == reloadCommand)
readPage();
else if (c == headCommand)
headPage();
}
}
Related examples in the same category