http - Send Input to a PHP screen from Java -
http - Send Input to a PHP screen from Java -
i want send info java php page. have googled bit code finding not working here illustration of code have used:
url url = new url("http://test.php"); urlconnection con = url.openconnection(); con.setdooutput(true); printstream ps = new printstream(con.getoutputstream()); ps.print("userid=12345"); ps.print("&password=test"); con.getinputstream(); ps.close(); what need input username , password on page , have php page take input , run log in function (simulate if user pressed button).
thanks ulrich
when seek utilize code simulate action done in browser , 1 of best api in java world httpclient, give many features create code deed user in front end of browser (fill input, click, load link ...) . here tutorial start.
but in case want utilize stardard java api without 3rd party library, there here few lines hope helpful :
first seek specify in url domaine name , page want invoke when requesting server ex:
url url = new url("http://www.mydomaine.com/test.php"); if website show popup authentication login/pass when trying access seek url instead :
url url = new url("http://login:password@www.mydomaine.com/test.php"); if show login form , seek access sending login , pass if fill input then, first thing identify name of input used when send request server , may login , password or else, find name seek inspect page browser (view source code of page or seek inspecting html elements) find :
<form action="login.php" method="post"> <input type="text" name="my_login"> <input type="password" name="my_password"> </form> let's suppose names my_login , my_password , in case the page have invoke login.php (which value of action form) code :
url obj = new url("http://www.mudomaine.com/login.php); httpurlconnection con = (httpurlconnection) obj.openconnection(); con.setrequestmethod("post"); string datas = "my_login=administrator&my_password=s3cred0n3"; con.setdooutput(true); dataoutputstream wr = new dataoutputstream(con.getoutputstream()); wr.writebytes(datas); wr.flush(); wr.close(); you can handle result calling con.getinputstream() if want manage server result .
but repeat 1 time again , if can utilize 3rd library utilize , because simplify job , , cut down possible bug in code.
java http
Comments
Post a Comment