http - Java send post request for login -
i want login application java code.
i have found following code example:
string requesturl = "http://applicationurl/login.jsp"; string data = "email=temail@mail.com&password=123password&login=login"; public static string sendpostrequest(string data, string requesturl) { string result=""; try { // send request url url = new url(requesturl); urlconnection conn = url.openconnection(); conn.setdooutput(true); outputstreamwriter writer = new outputstreamwriter(conn.getoutputstream()); //write parameters writer.write(data); writer.flush(); // response stringbuffer answer = new stringbuffer(); bufferedreader reader = new bufferedreader(new inputstreamreader(conn.getinputstream())); string line; while ((line = reader.readline()) != null) { answer.append(line); } writer.close(); reader.close(); //output response system.out.println(answer.tostring()); result = answer.tostring(); } catch (malformedurlexception ex) { ex.printstacktrace(); } catch (ioexception ex) { ex.printstacktrace(); } return result; }
but can not login, returns login page.
if can, please me understand doing wrong.
i have added method , content-type, still not work:
conn.setrequestmethod("post"); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded");
you didn't specify method you're using (get, post, ..). have here: sending http post request in java
maybe have set content-type:
connection.setrequestproperty("content-type", "application/x-www-form-urlencoded");
edit: try analyze http stuff using browser socket sniffer (e.g. wireshark). there might special cases like, cookies or server side verification stuff missed. hidden field being sent or that.. (it html form, right?)
Comments
Post a Comment