Java HttpClient: I keep getting login page back when I request a different webpage even after I had already logged in successfully -
my goal:
- post login
- get image (link) within same session , post
so far:
- i login via httppost , save cookie:
private string sessionid = ""; .... private int logintoserver() throws ioexception { int result = 0; string httpsurl = "http://192.168.1.100:8080/foo/login.jsp"; httpresponse response; closeablehttpclient httpclient = httpclients.createdefault(); httpclientcontext httpcontext = httpclientcontext.create(); try { httppost httppost = new httppost(httpsurl); list <namevaluepair> nvps = new arraylist <namevaluepair>(); nvps.add(new basicnamevaluepair("username", "*****")); nvps.add(new basicnamevaluepair("password", "*****")); httppost.setentity(new urlencodedformentity(nvps)); response = httpclient.execute(httppost,httpcontext); //store cookies cookiestore cookiestore = new basiccookiestore(); cookiestore = httpcontext.getcookiestore(); list<cookie> cookies = cookiestore.getcookies(); if(cookies != null) { for(cookie cookie : cookies) { sessionid = cookie.getvalue(); } } result = response.getstatusline().getstatuscode(); system.out.println(response.getstatusline()); httpentity entity = response.getentity(); entityutils.consume(entity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return result; }
- http/1.1 200 ok:
post /mysubdir/login.jsp http/1.1 content-length: 33 content-type: application/x-www-form-urlencoded host: 192.168.2.100:8080 connection: keep-alive user-agent: apache-httpclient/4.4.1 (java/1.8.0_25) accept-encoding: gzip,deflate username=******&password=******http/1.1 200 ok server: apache-coyote/1.1 set-cookie: jsessionid=97d93f7c7e11f22a6e895554e761d3ae; path=/foo/; httponly content-type: text/html;charset=iso-8859-1 transfer-encoding: chunked date: thu, 04 jun 2015 13:21:18 gmt 2000 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>hoarder login</title> <link rel="icon" href="resource/favicon.ico"></link> <meta http-equiv="content-type" content="text/html; charset=utf-8"></meta> <style type='text/css'> html, body { .margin: 0; .padding: 0; .overflow: hidden; } .....
- i send request:
private inputstream sendget(string url) { system.out.println("\nsending 'get' request url : " + url); httpget httpget = new httpget(url); httpresponse response = null; inputstream = null; try { //setting cookie store cookiestore cookiestr = new basiccookiestore(); basicclientcookie cookie = new basicclientcookie("jsessionid", sessionid); cookie.setdomain("192.168.1.100"); cookie.setpath("/foo/"); cookie.setattribute(clientcookie.path_attr, "/foo/"); cookie.setattribute(clientcookie.domain_attr, "192.168.1.100"); cookiestore cookiestr = httpcontext.getcookiestore(); cookiestr.addcookie(cookie); httpcontext.setcookiestore(cookiestr); closeablehttpclient httpclient = httpclients.custom().setdefaultcookiestore(cookiestr).build(); httpclient = httpclients.createdefault(); response = httpclient.execute(httpget); = response.getentity().getcontent(); system.out.println("response code : " + response.getstatusline()); entityutils.consume(response.getentity()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return is; }
- the response login page:
get /foo/images/b0df3a14706a-008-0008/7.jpg http/1.1 host: 192.168.1.100:8080 connection: keep-alive user-agent: apache-httpclient/4.4.1 (java/1.8.0_25) cookie: jsessionid=97d93f7c7e11f22a6e895554e761d3ae accept-encoding: gzip,deflate http/1.1 200 ok server: apache-coyote/1.1 cache-control: private expires: wed, 31 dec 1969 19:00:00 est set-cookie: jsessionid=d94c9147870ee8a7deedb67ad77b695e; path=/foo/; httponly content-type: text/html;charset=iso-8859-1 transfer-encoding: chunked date: wed, 03 jun 2015 18:28:02 gmt 2000 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>hoarder login</title> <link rel="icon" href="resource/favicon.ico"></link> <meta http-equiv="content-type" content="text/html; charset=utf-8"></meta> <style type='text/css'> html, body { .margin: 0; .padding: 0; .overflow: hidden; } ......
why getting this? can see sending request same session login. has had problem? , has came solution?
thank you.
i had similar experience. cookie getting rejected httpclient. solved code:
... cookiespecprovider easyspecprovider = new cookiespecprovider() { public cookiespec create(httpcontext context) { return new browsercompatspec() { @override public void validate(cookie cookie, cookieorigin origin) throws malformedcookieexception { // oh, easy } }; } }; registry = registrybuilder.<cookiespecprovider>create() .register("easy", easyspecprovider) .build(); closableclient = httpclient.setdefaultcookiespecregistry(registry).setdefaultrequestconfig(httprequestbase.getconfig()).build(); response = closableclient.execute(httprequestbase, context); ...
Comments
Post a Comment