Link to home
Start Free TrialLog in
Avatar of hoomanv
hoomanvFlag for Canada

asked on

post form data

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();
    }
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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

Avatar of hoomanv

ASKER

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)
>>im doing this already

Doesn't really look like it. What's in 'argString'?
Avatar of hoomanv

ASKER

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 ??
Avatar of hoomanv

ASKER

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&...
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
Avatar of hoomanv

ASKER

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
Avatar of hoomanv

ASKER

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
Avatar of hoomanv

ASKER

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 ?
>>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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hoomanv

ASKER

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 ?
>>you refered me to java.util.concurrent but that did not work

Please remind me of the url of that Q
Avatar of hoomanv

ASKER

https://www.experts-exchange.com/questions/21792606/The-process-does-not-terminate-nor-does-die.html

if i grade you in this thread ? would be able to post here again ?
I'll post there again irrespective of what happens here
:-)