how to add user & password in httpConnection?

[email protected] Sun, 21 Sep 2008 11:19:33 PDT
Newsgroups gmane.comp.java.sun.kvm
Message-ID <[email protected]>
hi,

I'm new in j2me,
i need it in my graduation project.

i have a problem in understand HttpConnection.
how can I connect to urls that need user name & password .?!


let say i want to build a j2me simple program, that display number of unread email in my hotmail email.

i read many toturials but it didn't work :(


this is my simple code which didn't work :(


can anybody tell me whats wrong ?


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import java.lang.*;
import java.util.*;


public class Email extends MIDlet 
implements CommandListener {

//The exit commands
private Command exitCommand; 



//The display for this MIDlet
private Display display;

Form displayForm;

public Email() {
display = Display.getDisplay(this);
exitCommand = 
new Command("Exit", Command.SCREEN, 1);

}




// Start the MIDlet by creating the Form and 
// associating the exit command and listener.
public void startApp() {
displayForm = new Form("");
displayForm.addCommand(exitCommand);
displayForm.setCommandListener(this);


try
{
//http://edugate.ksu.edu.sa/ksu/login

String result = getViaHttpConnection
("http://live.com/login");

displayForm.append(" " + result);



}
catch (Exception exc)
{
exc.printStackTrace();
}
display.setCurrent(displayForm);


}

public void pauseApp() { }

public void destroyApp(boolean unconditional) { }

public void commandAction(
Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}

}

String parse(String str)
{



}

String getViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
StringBuffer str = new StringBuffer();

try {
c = (HttpConnection)Connector.open(url);


String UserName = "";
String PassWord ="";

c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("IF-Modified-Since", "2 Oct 2002 15:10:15 GMT");
c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-CA");
c.setRequestProperty("Content-Length",""+ (UserName.length()+PassWord.length()));
c.setRequestProperty("UserName",UserName);
c.setRequestProperty("PassWord",PassWord);


// Get the ContentType
String type = c.getType();

// Getting the InputStream will open the connection
// and read the HTTP headers. They are stored until
// requested.
is = c.openInputStream();



// Get the length and process the data
int len = (int)c.getLength();
int ch;
while ((ch = is.read()) != -1) {
str.append((char)ch);
}

} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}




//String val = parse(str.toString());
System.out.println(str.toString());
return str.toString();
}



}
[Message sent by forum member 'dr_dream' (dr_dream)]

http://forums.java.net/jive/thread.jspa?messageID=300604

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff KVM-INTEREST".  For general help, send email to
[email protected] and include in the body of the message "help".