Skip to main content

Help on http response code 302

6 replies [Last post]
dex83
Offline
Joined: 2009-01-30
Points: 0

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class mjoystring extends MIDlet implements CommandListener {
private Form loginform;
private Display mydisplay;
private TextField username, password;
private Alert alert;

private Command CMD_SEARCH = new Command("Search",Command.OK,1);
private Command CMD_EXIT = new Command("Exit",Command.EXIT,1);

public void startApp() {

loginform = new Form("Mjoy Log-in");
username = new TextField("Username:","",15, TextField.ANY);
password = new TextField("Password:","",15, TextField.PASSWORD);

loginform.append(username);
loginform.append(password);
loginform.addCommand(CMD_SEARCH);
loginform.addCommand(CMD_EXIT);
loginform.setCommandListener(this);
mydisplay = Display.getDisplay(this);
mydisplay.setCurrent(loginform);

}

protected void destroyApp(boolean unconditional) {
notifyDestroyed();
}
protected void pauseApp() {}

public void commandAction(Command com, Displayable dis) {

if (com == CMD_EXIT) {
destroyApp(true);
} else
if (com == CMD_SEARCH) {
Thread t = new Thread() {
public void run() {
String mycookie = null;
Form waitform = new Form("Processing");
mydisplay.setCurrent(waitform);
try {
mycookie = searchstring();
//String mycookiedis = searchcookie2(searchstring());
//alert = new Alert("mjoy cookie",mycookiedis, null, null);
//alert.setTimeout(4000);
//mydisplay.setCurrent(alert);
}
catch (IOException e) {
alert = new Alert("mjoy cookie","Error na ito", null, null);
alert.setTimeout(4000);
mydisplay.setCurrent(alert);
}

String realcookie = searchcookie(mycookie);
String awts = "http://m.smart.com.ph@mjoy.com/m/login.htm;jsessionid" + searchcookie2(mycookie);

alert = new Alert("url1st",awts, null, null);
alert.setTimeout(4000);
mydisplay.setCurrent(alert);

try {
String myrealstring = realsearchstring(username.getString().trim(), password.getString().trim(), realcookie.trim(), awts);
alert = new Alert("Location",myrealstring, null, null);
//alert = new Alert("mjoy string",Integer.toString(myrealstring), null, null);
alert.setTimeout(4000);
mydisplay.setCurrent(alert, loginform);
}
catch (IOException e) {
alert = new Alert("mjoy String","Error na ito", null, null);
alert.setTimeout(8000);
mydisplay.setCurrent(alert, loginform);
}
}

};
t.start();
}
}
public String searchstring() throws IOException {
HttpConnection httpconnection;
InputStream inputstream;
httpconnection = null;
inputstream = null;
String s0 = "http://m.smart.com.ph@mjoy.com/m/login.htm";
httpconnection = (HttpConnection)Connector.open(s0);
httpconnection.setRequestMethod(HttpConnection.GET);
httpconnection.setRequestProperty("Host", "www.mjoy.com");
//httpconnection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
//httpconnection.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5,text/vnd.wap.wml;q=0.6");

//httpconnection.setRequestProperty("Accept-Charset", "ISO-8859-1,UTF-8;q=0.7,*;q=0.7");
//httpconnection.setRequestProperty("Keep-Alive","300");
//httpconnection.setRequestProperty("Proxy-Connection","keep-alive");
httpconnection.setRequestProperty("Referer","http://mjoy.com/m/login.htm");

//httpconnection.setRequestProperty("Accept-Language", "de,en;q=0.7,en-us;q=0.3");
httpconnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
String cookieholder = httpconnection.getHeaderField("Set-Cookie");
inputstream = httpconnection.openInputStream();
inputstream.close();
httpconnection.close();
return(cookieholder);
}

public String realsearchstring(String uname, String pword, String sessid, String sessids) throws IOException {
//String sessids = ";" + "jsessionid" + searchcookie2(sessid);
//String sessids = ";" + sessid.trim();
HttpConnection httpconnection;
InputStream inputstream;
OutputStream outputstream;
httpconnection = null;
inputstream = null;
//String s0 = "http://m.smart.com.ph@mjoy.com/m/login.htm;jsessionid" + sessids.trim();
//String s0 = "http://m.smart.com.ph@mjoy.com/m/login.htm"; sessids.trim();
//String s0 = "http://m.smart.com.ph@mjoy.com/m/login.htm" + ;
//alert = new Alert("URL",s0, null, null);
//alert.setTimeout(4000);
//mydisplay.setCurrent(alert);
String s1 = "nickname=" + uname + "&password=" + pword + "&action=Login";
httpconnection = (HttpConnection)Connector.open(sessids);
httpconnection.setRequestMethod(HttpConnection.POST);
httpconnection.setRequestProperty("Host", "www.mjoy.com");
httpconnection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
//httpconnection.setRequestProperty("Accept","text/xml,application/xml,application/xhtml+xml,te

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dex83
Offline
Joined: 2009-01-30
Points: 0

The problem here is the searchrealstring() where the response code is 302, but i cant get the right headerfield location, thats why i cant create a code for redirection,

i code httpcon.getHeaderField("Location") but it when i ouput it the string is not the real location but it says "Alert" without the quote.

Can someone answer why i get the value of Location = Alert, instead of the right location? Note the response code is 302.

Thanks in advance.

sfitzjava
Offline
Joined: 2003-06-15
Points: 0

Had a similar problem years ago. Try dumping all of the header fields to see if any have the data you might be looking for. Some webservers tend to not follow the standards Micro$oft, while some operators use gateways which rewrite some of the headers... to HELP you.

So you might find a header called: "Content-Location" that has the info you are looking for, if not the dump method will see if you have it somewhere else in the headers.

-Shawn

dex83
Offline
Joined: 2009-01-30
Points: 0

Sir, thank you for your reply. I see, but sir can you show me the example code how to dump the headers? How actualy i will get the header's content? I tried to change the ("location") to ("Server") but still i get the the "server = Alert" instead of "serves = Apache"
thank you in advance

sfitzjava
Offline
Joined: 2003-06-15
Points: 0

RTFM.... or for java I guess RTFJD

You need to "loop" through the headers... Hmm a "for" loop maybe?
A for loop uses an index which it can increment... Hmm.. now how could you look for
the value of a header based on an index??? But then how might you find the header key based on that same index?? So then you would be able to print the header key and it's value each time you looped. Of course if you got a NULL back you would be done and should break out of the loop.

While it would have been faster for me to just type out the 5 lines of code you need to figure this out for yourself. If you can't figure it out then you will never be able to solve the bigger issues you will be challenged with in JavaME.

-Shawn

PS. If you give a man a fish you have fed him for a day... teach a man to fish and you have fed him for a lifetime.

dex83
Offline
Joined: 2009-01-30
Points: 0

Thanks for the reply, i have finally got the idea,
i'll post here the results...

dex83
Offline
Joined: 2009-01-30
Points: 0

xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5,text/vnd.wap.wml;q=0.6");

//httpconnection.setRequestProperty("Accept-Charset", "ISO-8859-1,UTF-8;q=0.7,*;q=0.7");
httpconnection.setRequestProperty("Keep-Alive","300");
httpconnection.setRequestProperty("Proxy-Connection","keep-alive");
httpconnection.setRequestProperty("Referer","http://mjoy.com/m/login.htm");

//httpconnection.setRequestProperty("Accept-Language", "de,en;q=0.7,en-us;q=0.3");

httpconnection.setRequestProperty("Cookie", sessid.trim());
httpconnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
httpconnection.setRequestProperty("Content-length", Integer.toString(s1.length()));
outputstream = httpconnection.openOutputStream();
outputstream.write(s1.getBytes());
outputstream.flush();
outputstream.close();
int codeholder = httpconnection.getResponseCode();
alert = new Alert("mjoy string",Integer.toString(codeholder), null, null);
alert.setTimeout(4000);
mydisplay.setCurrent(alert, loginform);
inputstream = httpconnection.openInputStream();
String cookieholder = httpconnection.getHeaderField("Location");
inputstream.close();

httpconnection.close();

return(cookieholder);
}

private String searchcookie(String _string) {
int index = _string.indexOf(";");
String finalticket = _string.substring(0, index);
return(finalticket);
}

private String searchcookie2(String _string) {
int index = _string.indexOf(";");
String finalticket = _string.substring(10, index);
return(finalticket);
}

private String URLEncode(String s)
{
StringBuffer stringbuffer = new StringBuffer();
for(int j = 0; j < s.length(); j++)
{
int i = s.charAt(j);
switch(i)
{
case 32: // ' '
stringbuffer.append("+");
break;
case 33: // '!'
stringbuffer.append("%21");
break;
case 42: // '*'
stringbuffer.append("%2A");
break;
case 39: // '\''
stringbuffer.append("%27");
break;
case 40: // '('
stringbuffer.append("%28");
break;
case 41: // ')'
stringbuffer.append("%29");
break;

case 59: // ';'
stringbuffer.append("%3B");
break;
case 58: // ':'
stringbuffer.append("%3A");
break;
case 64: // '@'
stringbuffer.append("%40");
break;
case 38: // '&'
stringbuffer.append("%26");
break;
case 61: // '='
stringbuffer.append("%3D");
break;
case 43: // '+'
stringbuffer.append("%2B");
break;
case 36: // '$'
stringbuffer.append("%24");
break;
case 44: // ','
stringbuffer.append("%2C");
break;
case 47: // '/'
stringbuffer.append("%2F");
break;
case 63: // '?'
stringbuffer.append("%3F");
break;
case 37: // '%'
stringbuffer.append("%25");
break;
case 35: // '#'
stringbuffer.append("%23");
break;
case 91: // '['
stringbuffer.append("%5B");
break;
case 93: // ']'
stringbuffer.append("%5D");
break;
case 34: // '"'
case 45: // '-'
case 46: // '.'
case 48: // '0'
case 49: // '1'
case 50: // '2'
case 51: // '3'
case 52: // '4'
case 53: // '5'
case 54: // '6'
case 55: // '7'
case 56: // '8'
case 57: // '9'
case 60: // '<'
case 62: // '>'
case 65: // 'A'
case 66: // 'B'
case 67: // 'C'
case 68: // 'D'
case 69: // 'E'
case 70: // 'F'
case 71: // 'G'
case 72: // 'H'
case 73: // 'I'
case 74: // 'J'
case 75: // 'K'
case 76: // 'L'
case 77: // 'M'
case 78: // 'N'
case 79: // 'O'
case 80: // 'P'
case 81: // 'Q'
case 82: // 'R'
case 83: // 'S'
case 84: // 'T'
case 85: // 'U'
case 86: // 'V'
case 87: // 'W'
case 88: // 'X'
case 89: // 'Y'
case 90: // 'Z'
case 92: // '\\'
default:
stringbuffer.append((char)i);
break;
}
}
return stringbuffer.toString();
}
}