Akashdeep Saluja
Akashdeep Saluja

Reputation: 3089

How to pass input to a web page using a automated script

How to pass input to a php web page using a automated script ,i.e. i just want to know how pass arguments to text fields using a script. like passing input to username and password field of a web page and then pressing submit button(that too with a script).

favorable language: JAVA

Upvotes: 3

Views: 38847

Answers (4)

3ntr0py
3ntr0py

Reputation: 119

I would like to show how I would do to pass an input to the HTML. I usually use python to send request to the page where I need to input the data. Before doing that you need to know if you need to supply web-cookies or not, if yes, copy the cookie, if you need to be logged in otherwise not, just check that. Once that is done, you need to know the field names for the input area as you will be using them to POST or GET data using your script. Here is sample usage.

import urllib
import urllib2
import string
headers = {'Cookie': 'You cookies if you need'}
values = {'form_name':'sample text', 'submit':''}
data = urllib.urlencode(values)
req = urllib2.Request('website where you making request to',data,headers)

opener1 = urllib2.build_opener()


page1=opener1.open(req)
#OPTIONAL
htmlfile=page1.read()

fout = open('MYHTMLFILE.html', "wb")
fout.write(htmlfile)
fout.close()

Upvotes: -1

ddavison
ddavison

Reputation: 29032

Try Selenium. Selenium is great at automating web browsers.

http://seleniumhq.org/

Also has pure support with Java. But not only.

When it comes to custom methods, see ...

String urlParameters = "param1=a&param2=b&param3=c";
String request = "http://example.com/index.php";
URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);

DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();

source (Java - sending HTTP parameters via POST method easily)

Upvotes: 6

thedayofcondor
thedayofcondor

Reputation: 3876

You can use the Apache HTTPClient - see the example at:

http://hc.apache.org/httpclient-3.x/methods/post.html

This allows you to simulate submitting a fully filled form directly to the destination page and grab the results.

Remember that, after the call, you have to grab and store the session cookie in the response and resubmit it to the following pages you want to "visit" to stay "logged on"

Upvotes: 0

Gianluca Ghettini
Gianluca Ghettini

Reputation: 11628

if you web page uses the GET method to accept data (i.e. from URL), just connect to the web pages giving the data you want to pass:

http://www.mysite.com/mypage.html?data0=data0,data1=data1

if the web page uses POST things get a little bit more complicated: you have to forge an appropriate HTML request with all your data in the header (as POST method requires)

Upvotes: 1

Related Questions