aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Peculiar HTML Form INPUT Tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Peculiar HTML Form INPUT Tag" Watch "Peculiar HTML Form INPUT Tag" New topic
Author

Peculiar HTML Form INPUT Tag

Bill Denniston
Greenhorn

Joined: Aug 26, 2008
Posts: 6
I am trying to automate a form POST operation for a secure logon page. One of the form input tags looks like this:

<input type="hidden" name="a_name" value=""/>

The tag is all in lower case as shown. (Other INPUT tags in the same form are in upper case, but I didn't think that mattered.) The "/" following the empty string at the end of the tag is shown in red when the page source is viewed in Mozilla Firefox. When I use URLEncoder.encode("","UTF-8") to represent the empty string value in the POST string, nothing is inserted.

The POST operation otherwise transmits fine and returns a response fine, but the response indicates that the field values are incorrect and the logon is unsuccessful. I don't think I am properly representing the ""/ value in the POST string, and I think the red slash is a clue. Does anyone have any ideas or suggestions?

More generally, what is the proper way to represent an empty string value in a POST string?

Many thanks.

Bill Denniston
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42274
    
  64
Welcome to JavaRanch.

The slash indicates the closing tag of the INPUT element; its presence or absence has no bearing on the semantics of the element.

How are you sending the POST request - using the HttpUrlConnection class, the HttpClient library or some other means? Here's an example of using the former.
[ December 05, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Bill Denniston
Greenhorn

Joined: Aug 26, 2008
Posts: 6
Thanks for the reply. I now think there may be a cookie being sent with (an assumed) previous page load that I am not sending back in this post. (If so, I'm not sure how to find the cookie nor how to send it back with the POST; suggestions or examples appreciated.)

Here's my code. (I have neutralized the login username and password values, but the code otherwise runs fine as previously mentioned.)

import java.io.*;
import java.net.*;

public class URLTest {
public static void main(String[] args) throws Exception {

URL url = new URL("https://www.aopa.org/login/index.cfm?method=validate&WT.svl=LogBoxLogin");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches (false);
connection.setInstanceFollowRedirects(false);

String request = "username=" + URLEncoder.encode ("99999","UTF-8") +
"&password=" + URLEncoder.encode ("******","UTF-8") +
"&source=" + URLEncoder.encode ("secondary","UTF-8") +
"&requestUri=" + URLEncoder.encode ("/index.html","UTF-8");

OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write(request);
out.close();

BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));

String response;

while ((response = in.readLine()) != null) {
System.out.println(response);
}
in.close();
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42274
    
  64
If there are cookies involved -and especially if there's a back-and-forth of several requests and responses- then I'd advise to use the HttpClient library. It supports cookies between requests (which I think the HttpURLConnection class doesn't).

user docs

example code
[ December 05, 2008: Message edited by: Ulf Dittmer ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Peculiar HTML Form INPUT Tag