How to fill out POST HTML forms from my Java Application?

Hi -
I am writing a little Java GUI program that will allow a user to fill out data from the GUI and then submit that data. When they submit the data (two text fields) I want to take the two text fields and submit them via the POST form method of a HTML document over the web.
Here is what I got so far:
host = new URL("http", "<my_address>", web port, "/query.html");
            InputStream in = host.openStream();
            BufferedInputStream bufIn = new BufferedInputStream(in);
            for (;;)
                int data = bufIn.read();
                // Check for end of file
                if (data == -1)
                    break;
                else
                    System.out.print ( (char) data);
            }What that code does is makes a URL, opens a stream, and reads the HTML source of the file at the specified URL. There is a form that submits data via the POST method, and I would like to know how to write data to specific forms and specific input types of that form.
Then, I'd like to be able to read the response I get after submitting the form.
Is this possible?
Thanks,

Here is how one of my e-books go about Posting
I tryied in one of my projects and it works ok
(Tricks of the Java Programming Gurus)
There's another reason you may want to manipulate a URLConnection object directly: You may want to post data to a URL, rather than just fetching a document. Web browsers do this with data from online forms, and your applets might use the same mechanism to return data to the server after giving the user a chance to supply information.
As of this writing, posting is only supported to HTTP URLs. This interface will likely be enhanced in the future-the HTTP protocol supports two ways of sending data to a URL ("post" and "put"), while FTP, for example, supports only one. Currently, the Java library sidesteps the issue, supporting just one method ("post"). Eventually, some mechanism will be needed to enable applets to exert more control over how URLConnection objects are used for output.
To prepare a URL for output, you first create the URL object just as you would if you were retrieving a document. Then, after gaining access to the URLConnection object, you indicate that you intend to use the connection for output using the setDoOutput method:
URL gather = new URL("http://www.foo.com/cgi-bin/gather.cgi");
URLConnection c = gather.openConnection();
c.setDoOutput(true); Once you finish the preparation, you can get the output stream for the connection, write your data to it, and you're done:
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes("name=Bloggs%2C+Joe+David&favoritecolor=blue");
out.close();
//MY COMMENT
//This part can be improved using the URLEncoder
//******************************************************You might be wondering why the data in the example looks so ugly. That's a good question, and the answer has to do with the limitation mentioned previously: Using URL objects for output is only supported for the HTTP protocol. To be more accurate, version 1.0 of the Java library really only supports output-mode URL objects for posting forms data using HTTP.
For mostly historical reasons, HTTP forms data is returned to the server in an encoded format, where spaces are changed to plus signs (+), line delimiters to ampersands (&), and various other "special" characters are changed to three-letter escape sequences. The original data for the previous example, before encoding, was the following:
name=Bloggs, Joe David
favoritecolor=blue If you know enough about HTTP that you are curious about the details of what actually gets sent to the HTTP server, here's a transcript of what might be sent to www.foo.com if the example code listed previously were compiled into an application and executed:
POST /cgi-bin/gather.cgi HTTP/1.0
User-Agent: Java1.0
Referer: http://www.foo.com/cgi-bin/gather.cgi
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-type: application/x-www-form-urlencoded
Content-length: 43name=Bloggs%2C+Joe+David&favoritecolor=blue
Java takes care of building and sending all those protocol headers for you, including the Content-length header, which it calculates automatically. The reason you currently can send only forms data is that the Java library assumes that's all you will want to send. When you use an HTTP URL object for output, the Java library always labels the data you send as encoded form data.
Once you send the forms data, how do you read the resulting output from the server? The URLConnection class is designed so that you can use an instance for both output and input. It defaults to input-only, and if you turn on output mode without explicitly setting input mode as well, input mode is turned off. If you do both explicitly, however, you can both read and write using a URLConnection:
c.setDoOutput(true);
c.setDoInput(true); The only unfortunate thing is that, although URLConnection was designed to make such things possible, version 1.0 of the Java library doesn't support them properly. As of this writing, a bug in the library prevents you from using a single HTTP URLConnection for both input and output.
//MY COMMENTS
When you doing URL encoding
you should not encode it as one string becouse then it will encode the '=' signes and '&' signes also
you should encode all the field names and values seperatly and then join them using '&'s and '='s
Ex:-
public static void addField(StringBuffer sb,String name, String value){
   if (sb.length()>0){
      sb.append("&");
   sb.append(URLEncoder.encode(name,"UTF-8"));
   sb.append("=");
   sb.append(URLEncoder.encode(value,"UTF-8"));
}

Similar Messages

  • Fill out a pdf form from ADF application

    Hi
    I need to fill out a PDF form, from my ADF application and i want to know if exist a component or libarary to do this.
    i hope you can help me
    Regards.

    Hi,
    two options are:
    Oracle BI Publisher (by far best choice but needs to be licenced)
    http://www.oracle.com/technology/pub/articles/vohra-jdev-xmlpub.html
    Jasper Reports, Free but not as functional or easy to use as BIP
    http://jasperforge.org/projects/jasperreports
    Both products have Java API's which you can integrate with your application.
    Brenden

  • HT2506 I am trying to fill out a PDF form from an internet site, but when I go to print it, none of the content prints, only the header of the document.  Help!

    I am trying to fill out a PDF form from an internet site, but when I go to print it, none of the content prints, only the header of the document.  Help!

    Instead of printing it, try saving it as a pdf from the print menu, and then printing the pdf out.

  • How can i call a jasper report from a java Application

    Hi,
    i am chiranjit , currently i working in a web based ERP project, in this project as a report building tool we are using JasperReport wih eclipse plugin . in eclipse report's are generating very well but i am unable to call that report from a java application because i have no idea about the How to call a Jasper Report from a Java Application . so please send me the necessary class names, jar files names and programe code as early as possible.
    Chiranjit

    Ahh, kind of a duplicate thread:
    http://forums.java.sun.com/thread.jspa?threadID=631642
    @OP. You could have clarified your original post and the relationship of your question to java. You did not need a new thread.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How can i call a VB6 project from my java application using JNI

    hi
    can anyone tell me the procedure of calling a VB6 project from any java application using JNI
    if anyone does know then tell me the detail procedure of doing that. I know that i have to create a dll of that VB6 project then to call it from the java application.
    if anyone know that procedure of creating dll file of an existing VB6 project please reply
    please if anyone know then let me know

    Ahh, kind of a duplicate thread:
    http://forums.java.sun.com/thread.jspa?threadID=631642
    @OP. You could have clarified your original post and the relationship of your question to java. You did not need a new thread.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How can access MS Outlook Calender information  from my Java application.

    People schedule meeting with some data on regular basis.
    I need to access the Exchange server from my Java application and get the meeting dates along with other data pertaining to meeting.

    I had the same problem, and I dont think (as far as my knowledge goes) there is any freeware that will enable Java to access Exchange server. But there are some commercial products that are available.
    Chk this link as an example: http://www.compoze.com/products_hme_desc.html
    good luck in the research
    -kms

  • How to make use of Windows authentication from my Java application

    I have a Java application, Instead I design one more login page for my application, I want to make use of Windows Authentication.
    How should I use that windows authentication in my java application
    can any help me in suggesting a solution

    How will they be able to access your application if they aren't users of the system?

  • How can I launch a web browser from my Java application?

    I've reed about this at Question of the Week (http://developer.java.sun.com/developer/qow/archive/15/index.html). It doesn't work for me. I'm running win2000 and trying:
    try {
    Runtime.getRuntime().exec("start iexplore http://java.sun.com");
    } catch (Exception e) {
    System.out.println( e.getMessage() );
    I get:
    CreateProcess: start iexplore http://java.sun.com error=2
    What's wrong?

    This function will launch the default browser under Windows.
    public static void displayURL(String url) throws IOException   {
        String cmd = null;
        Process p;
        try  {
            String os = System.getProperty("os.name");     
            if (os != null && os.startsWith("Windows")) {
                if (os.startsWith("Windows 9") || os.startsWith("Windows Me"))  // Windows 9x/Me
                    cmd = "start " + url;
                else // Windows NT/2000/XP
                    cmd = "cmd /c start " + url;
                p = Runtime.getRuntime().exec(cmd);     
        catch(IOException x)        {
            // couldn't exec browser
            System.err.println("Could not invoke browser, command=" + cmd);

  • How to fill in a PDF form after client fills in HTML form

    i guess i need to try this a second time.
    i have been to numerous sites where they ask a user to fill in lot of fields with a html web form, and then when all done all that filled in data gets transferred into PDF forms for the client to print out, ertc
    tyopical sites i have seen this with are brokerage accounts for stock and commodities. all the major houses use this format, along with electronic signatures, etc ec etc
    so, the client fills out an html form, presses enter, and voila a pdf form shows up with all *its* fields filled in with the information the client supplied in the html form.
    this is what i am trying to figure out how to do. i have been searching the internet for 10 days now, about 4 hrs a day on this and i still have not found the code for this
    my site is PHP, but java i guess would work as well.
    any way, i am getting way behind on this project and if anybody knows where the code is to make this hapen at my site i'd be grateful to find out :)
    THANKS!!!

    Does anyone have any ideas on how to achieve this as we are keen to introduce this option on our own online forms?

  • Calling a web service from a Java application

    Does anyone have sample code showing how to call a web service over from a Java application? I'm deploy to HP-UX and seeking out the most standard and reliable approach.
    Thank you in advance.

    Keith,
    Download JWSDP 1.2, look at the tutorial for JAXRPC, especially
    the client portion.

  • Accessing custom Portal service from a java application

    We have a custom portal service that connects to BW using xmla. How do you access this portal service from a java application. Not from web dynpro, jsp or servlet but from the java code.
    Can we use the INITIAL_CONTEXT_FACTORY to get access to the portal service.
    Thank You
    D.K

    Now I tried the following:
    I've added the prtapi.jar and the service's jar to the additional-lib folder and added the appropriate entries to library.txt and reference.txt.
    Now I can obtain now the PortalRuntime, but this is not initialized.
    Has anybody a solution for this problem? Help would be high appreciated!
    Regards,
    Matthias

  • How can I embed an DOCTYPE HTML Form from Adobe Forms central into a responsive html5 page?

    How can I embed an DOCTYPE HTML Form from Adobe Forms central into a responsive html5 page?
    -Luis

    Hi,
    You can embed the form on your website, but you need to make sure that javascript has been enabled in the browser. You need to copy the embed code and add it into your HTML code. If you would like FormsCentral to generate embeded HTML form without using javascript, you may post a feature request and vote it. Hope it helps! Thanks!
    Kind regards,
    Shiyao Bao

  • Open pdf says "please fill out the following form" when I attempt to apply redactions, I get an error message.  How do I "turn off" the form portion of the document?  I assume that is the problem since I have not had this issue with any of the other files

    I am attempting to redact a large pdf file.  When I open the file, it has a bar across the top with the message "please fill out the following form".  I can mark redactions, but when I attempt to apply them I get a message saying, "In order to proceed Digital Signatures must be removed and the document must be fully authorized.  Do you want to make these changes and continue?"   I click "ok".  Then I get a message saying, "an internal error occurred."  How do I get around this problem?

    You cannot redact a signed document. This is because one of the things signatures guarantee is access to the OLDER document (i.e. there will be an unredacted copy in there).
    In general you cannot remove signatures, but sometimes you can. Look under the Signatures tab (right click on left vertical bar if there isn't one) to see if it is removable.

  • Can we create an Eloqua cookie for a user if we just know his email address and they have not clicked an email link or filled out an Eloqua form?

    Scenario:
    When a visitor comes to our website and creates a login, we are pushing their profile information to Eloqua via the Eloqua web services api. However, because they have not filled out an Eloqua form or clicked through an email, their page visit activity is not synced with their profile information in Eloqua because they don't have an Eloqua cookie yet (for first time visitors only).
    I know you can integrate an external form with an Eloqua form (which will then create the Eloqua cookie automatically) but because this is a login form for our website, we don't want to send secure information (such as passwords) over to Eloqua.
    Is there a way to create the Eloqua cookie using the web services api and/or javascript or is this a missing feature?

    I think what you're asking is how can you link a visitor profile to an Eloqua contact.  An Eloqua cookie is placed on a user's computer by the javascript tracking scripts - this is just a unique id that is used when saving the visitor information to a table in the Eloqua database.  It will be there regardless of whether the user comes from an email or submits a form.  What does happen when a user visits your website from an Eloqua email or submits a form is the visitor profile that has been keeping track of website activity in Eloqua gets linked to an Eloqua contact by email address.
    To make this link happen in your scenario, you can instead of pushing the information through the web services to Eloqua directly as a create contact call, create a form submission through the web services where you can pass through the elqCustomerGUID (the cookie value) along with at least an email address to make the link happen.
    You will need to:
    1. use javascript on your website to pull the elqCustomerGUID from eloqua.  There's code for this in the integration details for individual forms in Eloqua.  It has to be pulled from Eloqua servers because the cookie is a third party cookie.
    2. pass this cookie value to your server so you have it when you make the web services calls to Eloqua to push the contact information over and now make a form submission record (entities through the api Base->Form->*this will be a form submission record*) to create the link in Eloqua.

  • Filling out a pdf form with preview

    I need to fill out some government forms which are of course in a pdf format and they want them typed.  Before i go looking for a typewriter at the antique store, isn't there a way to do it and print it retaining the forms format?  Preview will allow me to do about 3 lines and then stops allowing input.  I've tried the google crap and from there the apache odt crap.  Tried it on a PC too with growing frustration and zero accomplished.

    Preview will allow me to do about 3 lines and then stops allowing input.
    That is quite strange. I fill out pdf forms with Preview, and it allows me to put as much text in as I want. I wonder if the problem is something inherent in the particular form you are trying to annotate. You might try Skim ( Download Skim for Mac - PDF Reader and note-taker for scientific papers. MacUpdate.com ) which is an app I used to use to annotate pdfs until I discovered that that functionality was already present in Preview. I don't know if you can post that form here or elsewhere and I could try to annotate it with Preview.

Maybe you are looking for