Results 1 to 7 of 7
  1. #1
    moral is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Fill the html form

    It's possible fill the http form from Java?
    I must send multiple files to http form on www side (source code is):
    Java Code:
    <div id="fileupload"> 
        <form method="post" action="Upload.aspx" id="aspnetForm" enctype="multipart/form-data">
        <input type="file" name="SendFile" id="se"><br>
         <input type="submit" name="send" value="  Send  " onclick="return openWindow();">
         div id="pan" style="display :none;">
         Sending, wait... <br>
         <span class="lab">File: </span> <span id="nameFile">not selected.</span>
       </div>
       <script type="text/javascript" language="javascript">
       //<![CDATA[
       function openWindow()
        {
           var msg = document.getElementById("ContentBody_idCtrlMsgBox_PanelMsg");
            if( msg != null && typeof( msg ) == "object" )
            msg.style.display = "none";
            var g = document.getElementById("se").value;
            if( g != "" )
            document.getElementById("nameFile").innerHTML = g;    
            document.getElementById("fileupload").style.display = "none";
            document.getElementById("pan").style.display = "block";  
            return true;
         }
       //]]>
        </script>
         ...
         ...
         </form>
    I tried the Apache HTTPClient but without succes:-(

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Fill the html form

    Is this a java programming problem? Do you have java code that you are working on?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Fill the html form

    Given the reference to HTTPClient, looks like this question is about submitting to a webpage from a Java application, basically to mimic what the posted form does.

    But yeah, without any kind of code or a produced error, there is nothing to answer here.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    moral is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Fill the html form

    Quote Originally Posted by Norm View Post
    Is this a java programming problem? Do you have java code that you are working on?
    Yes I tried use:
    Java Code:
     public void testUpload() throws Exception {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://address");
    
        MultipartEntity reqEntity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);
    
        //reqEntity.addPart("string_field",
        //    new StringBody("field value"));
    
        FileBody bin = new FileBody(
            new File("D:\\Temporary\\20131104_2.xml"));
        reqEntity.addPart("SendFile", bin );
    
        httppost.setEntity(reqEntity);
    
        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
    
        if (resEntity != null) {
            String page = EntityUtils.toString(resEntity);
            System.out.println("PAGE :" + page);
        }
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Fill the html form

    What happens when the code is executed? What does the server receive?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    moral is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Fill the html form

    Quote Originally Posted by Norm View Post
    What happens when the code is executed? What does the server receive?
    I create on localhost form for test the html is:

    <form enctype="multipart/form-data" method="post" action="doSom.php" name="fileForm"> <br>
    <div style="text-align: justify;">
    File to send:<input name="fileFile" type="file"><br>
    </div>
    <div style="text-align: justify;">
    <input value="Send" type="submit" name="Sub">
    </div>
    </form>

    And the php code:

    <h1>Send file....</h1>
    <?php
    echo 'Name of file: '.$_FILES['fileFile']['name'].'<br/>';
    if ($_FILES['fileFile']['error'] > 0){
    echo 'Error: ';
    switch($_FILES['fileFile']['error']){
    case 1: echo 'Error 1'; break;
    case 2: echo 'Error 2'; break;
    case 3: echo 'Error 3'; break;
    case 4: echo 'File not send'; break;
    }
    exit;
    }



    if ($_FILES['fileFile']['type'] != 'text/xml'){
    echo 'Bad content';
    exit;
    }




    $path = 'files/'.$_FILES['fileFile']['name'];

    if(is_uploaded_file($_FILES['fileFile']['tmp_name'])){
    if(!move_uploaded_file($_FILES['fileFile']['tmp_name'], $path)){
    echo 'Error in move to temporara directory';
    exit;
    }
    }else{
    echo 'Error: hacking into a web page: '.$_FILES['fileFile']['name'];
    exit;
    }

    echo 'File send<br/><br/>';
    ?>

    When i test the program on localhost file is upload when I send file directly to php code not to html form and when I comment the:
    if ($_FILES['fileFile']['type'] != 'text/xml'){
    echo 'Bad content';
    exit;
    }
    But the real problem is diferent the side is code in asp and in my opinion I need emulate a web browser?

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Fill the html form

    Could be that the ASP site validates the HTTP headers to try and detect abuse. And if the site does that, then it is likely that what you are trying to do here is actually prohibited. But if you're sure that you are not doing anything against the site rules, you likely need to look into setting a proper 'referer' HTTP header.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 6
    Last Post: 04-06-2011, 12:39 PM
  2. HTML Form
    By johnmergene in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 02:23 AM
  3. Html form
    By redforce.bala in forum Advanced Java
    Replies: 1
    Last Post: 10-04-2010, 02:07 PM
  4. Replies: 10
    Last Post: 08-12-2009, 04:02 PM
  5. HTML Form
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •