
import com.nttdocomo.ui.*;
import com.nttdocomo.util.*;
import java.io.*;
import javax.microedition.io.*;
import com.nttdocomo.io.*;



public class HttpAccess extends IApplication
	implements ComponentListener
{
	TextBox inputbox = new TextBox("",16,1,TextBox.DISPLAY_ANY);
	TextBox outputbox = new TextBox("",16,6,TextBox.DISPLAY_ANY);
	Button connectButton = new Button("CONNECT");


    public void start()
	{
		Panel panel = new Panel();

		panel.add(new Label("LABEL",Label.CENTER));
		panel.add(inputbox);
		panel.add(connectButton);
		panel.add(outputbox);
		outputbox.setEditable(false);

        Display.setCurrent(panel);

		panel.setComponentListener(this);

    }


	public void componentAction(Component source, int type, int param) 
    {
		System.out.println("CE:" + source + "/" + type + "/" + param);

		if(source == connectButton)
		{
			String message = inputbox.getText();

			httpAccess(message);
		}
	}


	public void httpAccess(String message)
	{
		String response = null;

		try
		{
			String urlString = "http://localhost/ia/httpaccess/echo.cgi" + "?prm=" + message;

			HttpConnection connection = (HttpConnection)Connector.open(urlString,Connector.READ);
			//connection.setRequestProperty("Content-type","text/plain");
			connection.setRequestMethod(HttpConnection.GET);
			connection.connect();

			InputStream in = connection.openInputStream();
			byte b[] = new byte[0x100];
			while(true)
			{
				int len = in.read(b);
				if(len < 0) break;
			}
			in.close();
			connection.close();

			response = new String(b);
			response = response.substring(0,response.indexOf("\0"));//文字列の後ろ切り取りはいいのか。はて？

			outputbox.setText(response);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}



