[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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();
    }
}
0
hoomanv
Asked:
hoomanv
  • 8
  • 8
1 Solution
 
CEHJCommented:
It's not really related to a <form> Check

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

0
 
hoomanvAuthor 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)
0
 
CEHJCommented:
>>im doing this already

Doesn't really look like it. What's in 'argString'?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Please remind me of the url of that Q
0
 
hoomanvAuthor 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 ?
0
 
CEHJCommented:
I'll post there again irrespective of what happens here
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now