I dont think this is possible to do, so perhaps I need a totally new way of looking at the problem.
My java program connects to a web form and logs in. I have the username and password form another part of the code.
The HTML form will contain different input types obviously. Depending on the user, and the site, each field will be named differently. Here’s a sample web form:
<FORM METHOD=POST ACTION="/my.form">
Username:
<INPUT NAME="username">
<BR>
Password
<INPUT TYPE="PASSWORD" NAME="password">
<INPUT TYPE="HIDDEN" NAME="my-hidden-name" VALUE="someValue">
<BR><INPUT TYPE="SUBMIT" VALUE="Login">
</FORM>
In this form, the NAME fields can be anything and will change form site to site. I’ve gotten around this by requiring a PROPERTIES file that stores the fields, eg:
username
password
my-hidden-name=someValue
SUBMIT=Login
This works for half of the above fields. The problem is the username and password fields.
I already have the username and password, so I COULD just do: if(currentField=="password") string=mypass123. but then what if someone makes a form where the username/password field is called user1 or passwordField, or basically anything other than what I’ve put in the if()!!
So, can anyone suggest a way I can handle any combination of names, and still identify which ones are the username and password fields? Here’s my current code:
//Read file logic here
Properties formProps=getDataFromResource(pathToFormFieldsPropfile);
if(null!=formProps)
{
//Gets a list of all the field names, eg "username", "password", "submit"
Enumeration<?> formFields=formProps.keys();
while(formFields.hasMoreElements())
{
String tempString=(String) formFields.nextElement(); //This gets the field name
query+=tempString //Will contain field name
+"=" //Used as a divider in the querystring, ie field=value
+formProps.get(tempString) //Value corresponding to field
//+ URLEncoder.encode((String) formProps.get(tempString),Charset.defaultCharset().toString()) //The value needs to be encoded for POST
+"&"; //The trailing one of these will have to be removed at the end
}
}
Thanks!
Just to follow up on this, I managed to come up with a way to fix this, but its probably not ideal.
Basically, I make it so the user must have the username and password as the first and second blank entries in the PROPERTIES file.
Then in my code I have a counter that says,
if ((counter++)==0), insert usernameand thenif ((counter++)==1), insert password, followed byelse leave it blank.This works for what I need to do, but may not be ideal.
PS, I implemented a custom Property class to make sure they’re all ordered, as the default method stores them in an unordered list.