Skip to main content

how to add user & password in httpConnection?

1 reply [Last post]
dr_dream
Offline
Joined: 2008-09-21
Points: 0

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

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Jac Kersing

On Sun, 21 Sep 2008, meinterest@mobileandembedded.org wrote:

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

One minute of googling suggests this page may have the answer you're
looking for:
http://www.java2s.com/Code/Java/J2ME/Asimplenetworkclient.htm

Best regards,

Jac

---
Jac Kersing Technical Consultant The-Box Development
j.kersing@the-box.com CISSP RHCE http://www.the-box.com

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".