Source Code : Http Test

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

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() + "
Protocol: " + c.getProtocol()
          + "
Host: " + c.getHost() + "
File: " + c.getFile()
          + "
Ref: " + c.getRef() + "
Query: " + c.getQuery()
          + "
Port: " + c.getPort() + "
Method: "
          + c.getRequestMethod());
      InputStream is = c.openInputStream();
      // DEBUG: request System.out.println(b) ;

      b.append("
ResponseCode: " + c.getResponseCode()
          + "
ResponseMessage:" + c.getResponseMessage()
          + "
ContentLength: " + c.getLength() + "
ContentType: "
          + c.getType() + "
ContentEncoding: " + c.getEncoding()
          + "
ContentExpiration: " + c.getExpiration() + "
Date: "
          + c.getDate() + "
Last-Modified: " + c.getLastModified()
          + "

");

      int h = 0;
      while (true) {
        try {
          String key = c.getHeaderFieldKey(h);
          if (key == null)
            break;
          String value = c.getHeaderField(h);
          b.append(key + ": " + value + "
");
          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( ) + "
Protocol: " +
   * c.getProtocol() + "
Host: " + c.getHost() + "
File: " + c.getFile() +
   * "
Ref: " + c.getRef() + "
Query: " + c.getQuery() + "
Port: " +
   * c.getPort() + "
Method: " + c.getRequestMethod()) ;
   * 
   * System.out.println( "Gonna openInputStream... " ); is =
   * c.openInputStream();
   * 
   * System.out.println( "Going to get Properties after OpeninputStream" );
   * b.append( "
ResponseCode: " + c.getResponseCode() + "
ResponseMessage:" +
   * c.getResponseMessage() + "
ContentLength: " + c.getLength() +
   * "
ContentType: " + c.getType() + "
ContentEncoding: " + c.getEncoding() +
   * "
ContentExpiration: " + c.getExpiration() + "
Date: " + c.getDate() +
   * "
Last-Modified: " + c.getLastModified() + "

" );
   * 
   * 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 + "
" ); 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();
  }

}




           
       

Thank with us