I am implementing http GET/PUT methods over socket in a mIdlet.

I think I am able to connect to the server(since the connection object is formed) but am not able to read the file(inputstream.read() returns -1). I am testing on a emulator and the code does’nt throw any exception but it does’nt display the file either.I tested the code with a http protocol on emulator and it works fine.Cud you give me any hint on this.

i am pasting my code for your reference.

import java.io.*;

public class HttpPut extends MIDlet {

// StreamConnection allows bidirectional communication

private StreamConnection streamConnection = null;

// use OutputStream to send requests

private OutputStream outputStream = null;

private DataOutputStream dataOutputStream = null;

// use InputStream to receive responses from Web server

private InputStream inputStream = null;

private DataInputStream dataInputStream = null;

// specify the connect string

private String connectString = "socket://";

// use a StrignBuffer to store the retrieved page contents

private StringBuffer results;

// define GUI components

private Display myDisplay = null;

private Form resultScreen;

private StringItem resultField;

public HttpPut() {

// initializing GUI display

results = new StringBuffer();

myDisplay = Display.getDisplay(this);

resultScreen = new Form("Page Content:");


public void startApp() {

try {

// establish a socket connection with remote server

streamConnection =

(StreamConnection) Connector.open(connectString);

// create DataOuputStream on top of the socket connection

outputStream = streamConnection.openOutputStream();

dataOutputStream = new DataOutputStream(outputStream);

// send the HTTP request

dataOutputStream.writeChars("GET /index.html HTTP/1.1\n");
dataOutputStream.writeChars("Host: localhost\r\n\r\n");

// create DataInputStream on top of the socket connection

inputStream = streamConnection.openInputStream();

dataInputStream = new DataInputStream(inputStream);

// retrieve the contents of the requested page from Web server

int inputChar;

while ( (inputChar = dataInputStream.read()) != -1) {

results.append((char) inputChar);


// display the page contents on the phone screen

resultField = new StringItem(null, results.toString());

System.out.println("Results is " + results.toString());



} catch (IOException e) {

System.err.println("Exception caught:" + e);

} finally {

// free up I/O streams and close the socket connection