android - Why doesn't HttpClient work but HttpUrlConnenction do when posting data to form -
i'm building android app should perform on site 2 cookies , perform post same site these cookies.
as mentioned start of , i'm using org.apache.http.client.httpclient perform operation.
string requiredcookies = ""; httpcontext localcontext = null; system.out.println("------------------get----------------------"); httpclient httpclient = new defaulthttpclient(); httpget = new httpget("www.mysitegeturl.com"); //creating local instance of cookie store. cookiestore cookiejar = new basiccookiestore(); // creating local http context localcontext = new basichttpcontext(); // bind custom cookie store local context localcontext.setattribute(clientcontext.cookie_store, cookiejar); httpresponse response; try { response = httpclient.execute(get, localcontext); httpentity entity = response.getentity(); system.out.println(response.getstatusline()); if (entity != null) { system.out.println("response content length: " + entity.getcontentlength()); } //do java.net impl should work list<cookie> cookies = cookiejar.getcookies(); (int = 0; < cookies.size(); i++) { requiredcookies += cookies.get(i).getname()+"="+cookies.get(i).getvalue()+";"; } if (entity != null) { entity.consumecontent(); } } catch (clientprotocolexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } system.out.println("------------------get-end---------------------");
so far good. don't mind requiredcookies line yet, used in java.net impl since can't httpclient 1 work =(. let's take @ non working httpclient post part.
system.out.println("------------------httpclient - post----------------------"); httppost post = new httppost("www.mysiteposturl.com"); //params httpparams params = new basichttpparams(); params.setparameter("foo", "post"); params.setparameter("bar", "90"); params.setparameter("action", "search"); post.setparams(params); post.setheader("content-type", "application/x-www-form-urlencoded"); try { httpresponse response2 = httpclient.execute(post, localcontext); system.out.println(response2.getstatusline()); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("------------------post end---------------------");
what happens perform post localcontext cookies stored. doesn't work. http/1.1 401 no session. since had no luck tried approach(java.net.httpurlconnection). remember still use same part
url url = new url("www.mysiteposturl"); httpurlconnection connection = null; string datastring = "bar=90&foo=post&action=search"; try { connection = (httpurlconnection)url.openconnection(); connection.setrequestproperty("cookie", requiredcookies); //set post connection.setdooutput(true); writer writer = new outputstreamwriter(connection.getoutputstream()); writer.write(datastring); writer.flush(); writer.close(); connection.connect(); if (connection.getresponsecode() == 200 || connection.getresponsecode() == 201) { system.out.println(connection.getcontent().tostring()); } else { system.out.println("error"); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("------------------post end---------------------");
and viola 200 displayed , works charm. guys think? please provide me answer because can't figure out.
the problem appears have 2 different host names in setup. cause http client not send cookies different host. try changing domain of cookies in cookie store, or using same host , post. additionally manually add cookies headers in http client did in httpurlconnection example.
Comments
Post a Comment