HTTP Request with Cookie grab

91 views
Skip to first unread message

bucher...@gmail.com

unread,
Dec 16, 2014, 2:28:33 AM12/16/14
to codenameone...@googlegroups.com
Hi

i'm a new user of Codename One. We have a look for a 'Hybrid Framework' to develop apps for our company. In fact of that i try to make a pilot app.

But now concret: I would make a HTTP Post with credentials. Then I would grab the cookies or session information. Than I need these Informations for further requests.

I have a working Java Example:

String userId = "test";
String pwd = "test";
String nextPage="%2Fwf%2Fservlet.method%2Fcom.groiss.smartclient.Main.start";
String cookies = "";


HttpURLConnection uc;
try {
uc = (HttpURLConnection)new URL(url).openConnection();
uc.setRequestMethod("POST");
uc.setDoOutput(true);
uc.setInstanceFollowRedirects(false);
uc.connect();
OutputStream os = uc.getOutputStream();
// post user and password 
os.write(("avw_user="+userId+"&avw_password="+URLEncoder.encode(pwd,"UTF8")+"&nextPage="+nextPage).getBytes());
int cd = uc.getResponseCode();
if (cd != 200 && cd != 302) {
System.out.println("Error: "+uc.getResponseMessage());
}
InputStreamReader is = new InputStreamReader(uc.getInputStream());
String scookies = uc.getHeaderField("Set-Cookie");

StringBuilder cs = new StringBuilder();
String headerName=null;
for (int i=1; (headerName = uc.getHeaderFieldKey(i))!=null; i++) {
System.out.println("Header Name: " + headerName + "/t" + "Header Field: " + uc.getHeaderField(i));
System.out.println("/n");
if (headerName.equals("Set-Cookie")) {                  
cs.append(uc.getHeaderField(i));
}
}

cookies = cs.toString();

} catch (MalformedURLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println(cookies);
System.out.println("\n");
HttpURLConnection uc2;
try {
uc2 = (HttpURLConnection)new URL(url2).openConnection();
uc2.setRequestMethod("GET");
uc2.setRequestProperty("Cookie", cookies);
uc2.connect();
int cd = uc2.getResponseCode();
if (cd != 200 && cd != 302) {
System.out.println("Error: "+uc2.getResponseMessage());
}

String headerName=null;

InputStreamReader is = new InputStreamReader(uc2.getInputStream());
String sOut = "";
int data = is.read();
while(data != -1){
sOut += (char) data;
   data = is.read();
}

is.close();
System.out.println(sOut);
System.out.println("\n");
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

How can I do the same with Codename One? Should I use the Class 'ConnectionRequest'?

Chen Fishbein

unread,
Dec 16, 2014, 9:20:29 AM12/16/14
to codenameone...@googlegroups.com, bucher...@gmail.com
Yes, take a look at NetworkManager and ConnectionRequest

bucher...@gmail.com

unread,
Dec 17, 2014, 1:40:11 AM12/17/14
to codenameone...@googlegroups.com, bucher...@gmail.com
Have you got an example for a http post request? I have found a lot examples for http get but no post.

Shai Almog

unread,
Dec 17, 2014, 1:50:08 AM12/17/14
to codenameone...@googlegroups.com, bucher...@gmail.com
The default is post. Just use setPost(true) and the request will be a post request with the exact same API.

bucher...@gmail.com

unread,
Dec 17, 2014, 2:37:25 AM12/17/14
to codenameone...@googlegroups.com, bucher...@gmail.com
Okay, I will try that. I give than a Feedback how it works.

bucher...@gmail.com

unread,
Jan 15, 2015, 1:36:43 AM1/15/15
to codenameone...@googlegroups.com, bucher...@gmail.com
I have allready the problem with the cookie grab.

I made an post-request with credentials. It seems that this will work, because I get a response from the server. During the request, I took the session id out of the cookies.

In the second step I will make an get-request with the session id from before.

In "normal" java it works. I make something like this:
HttpURLConnection con = (HttpURLConnection)new URL(url2).openConnection();
con .setRequestMethod("GET");
con .setRequestProperty("Cookie", cookies);

I have tried with addRequestHeader but it would not work. I get an not autorized error.

Have you got any idea?

Shai Almog

unread,
Jan 15, 2015, 11:16:36 AM1/15/15
to codenameone...@googlegroups.com, bucher...@gmail.com
If you want to override the cookies from the server try overriding:
cookieReceived, cookieSent & initCookieHeader,

bucher...@gmail.com

unread,
Jan 26, 2015, 2:21:54 AM1/26/15
to codenameone...@googlegroups.com, bucher...@gmail.com
Hi together

now it works. I have made the error by myself. I have sended some wrong information in the post Request.
Thanks for helping.

best regards
Reply all
Reply to author
Forward
0 new messages