We help IT Professionals succeed at work.

post form data

hoomanv
hoomanv asked
on
Medium Priority
235 Views
Last Modified: 2010-03-31
there is a poll form on the web that posts data to an ASP page
I created a html file localy and i am able to post form data just like this

<form name="frm" method="post" action="the url">
    <input type="hidden" name="voteChoice" value="498">
    <input type="hidden" name="PID" value="108">
    <input type="hidden" name="TID" value="7714">
    <input type="hidden" name="FID" value="33">
    <input type="hidden" name="PN" value="0">
    <input type="hidden" name="TPN" value="1">
    <input type="submit" name="Submit" value="Submit">
</form>

when I want to do this via java with the below code its not successfull to update the poll. i dont know why ?

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

public class PostForm {
    public static void main(String[] args) throws Exception {
        URL url = new URL("the url");
        String argString = "voteChoice=498&PID=108&TID=7714&FID=33&PN=0&TPN=1";
       
        argString = "voteChoice=498&PID=108&TID=7714&FID=33&PN=0&TPN=1";
       
        URLConnection con = url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestProperty("Referer", "http://www.google.com");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
       
        // Write the arguments as post data
        OutputStream out = con.getOutputStream();
        out.write(argString.getBytes());
        out.flush();
        out.close();
       
        // Read the response
        InputStream is = con.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
It's not really related to a <form> Check

http://javaalmanac.com/egs/java.net/Post.html

Top Expert 2006

Author

Commented:
im doing this already
my problem is somthing else
what is the difference between the html file and my java app
when a browser parses the html it would do just somthing like i do
but somthing in the server side i guess prevents the java app
after sobmitting that poll server will send me the updated poll
if i use the html version, it updates, but using java it just send me the current poll
should i explain more ? (sorry for my bad english)
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>im doing this already

Doesn't really look like it. What's in 'argString'?
Top Expert 2006

Author

Commented:
i made the previous question because i though the ASP page rejects unknown submitters and that was why i changed the referer and user-aganet. but nothing helpd. as a guess is there anything else is submitted by the browser that im forgetting in my java app. for example cookies ??
Top Expert 2006

Author

Commented:
the argString is the encoded version of all the arguments that have to be sent. any GET and POST request should first convert the argument list to somthing like this
foo=bar&a=b&...
CERTIFIED EXPERT
Top Expert 2016

Commented:
Quite possibly - cookies are often required, but the request has also to be formed correctly. You should probably inspect what your browser is sending using a proxy such as Eclipse+Solex
Top Expert 2006

Author

Commented:
you mean i need to install a proxy server and make my request through it to find out what is going on exactly ?
before that i first clear my cookies and test again, then i use another browser and test again
Top Expert 2006

Author

Commented:
and i forgot to say the request is correct 99%, because I'll get to the poll page by providing varibales PID,TID,FID,... (these are form inputs),  but the only failure is that the "voteChoice"="498" seems not working correctly in java but it does in borwser
Top Expert 2006

Author

Commented:
ok i got good results
i was unable to post the form using MS IE, simply because I have already signed in to that site using FireFox, and there are 4 cookies set by the site in my borwser. so only I can use FireFox to post the form. so i ask another question. how can i login to that site using java and permit it to set cookies, how can i use the currently set cookies to comunicate with that site to authorize my java app and post the form. generally how exactly the server requests the cookie from browser ?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>you mean i need to install a proxy server and make my request through it to find out what is going on exactly ?

That's really the only way to find out precisely what happens in your browser
CERTIFIED EXPERT
Top Expert 2016
Commented:
You need to hit a main page first and collect the cookies. You'll find all this much easier with a proper http library such as Jakarta Commons HttpClient

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2006

Author

Commented:
yes thats it.

HttpClient supports automatic management of cookies, including allowing the server to set cookies and automatically return them to the server when required. It is also possible to manually set cookies to be sent to the server.

A * 20 points for a name.

though you helped me today, yesterday i just consume 125 points because i was a newbie and unfamiliar with grading system. that day i asked about non-blocking IO and you refered me to java.util.concurrent but that did not work i found that only chanels have the ability to be interrupted not traditional io, if im wrong tell me, but my problem is still alive. the availeble() method does not work either and the call to this method also blocks on foregoing circumstances. i cant get the inputstream out of block at all. do you have any suggestions ?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>you refered me to java.util.concurrent but that did not work

Please remind me of the url of that Q
Top Expert 2006

Author

Commented:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21792606.html

if i grade you in this thread ? would be able to post here again ?
CERTIFIED EXPERT
Top Expert 2016

Commented:
I'll post there again irrespective of what happens here
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.