Results 1 to 11 of 11
  1. #1
    Darrarski is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default HTTP Post Request from Java

    I'm working on Java application that has to upload some files to the Apache server with PHP. I've found this article about using HTTP POST's requests from Java, and it's the best solution I've found over the net - simple and not using any extra libraries.
    Send Form Data from Java: A Painless Solution
    The problem is that it's working only with my local webserver. When I'm trying to upload some data to server over the net, I'm getting exception, where ex.getMessage() equals given host address. I've already checked firewall etc, and there is problem somewhere else.
    This is the java code I'm using to test POST:
    Java Code:
    InputStream serverInput = ClientHttpRequest.post(
    	new java.net.URL("http://localhost/upload.php"),
    	new Object[] {
    		"test1", "value1",
    		"file", new File("/path/to/my/file.pdf")
    	}
    );
    And there is PHP script to test if everything is working:
    Java Code:
    <?php
    $output = print_r($_POST, true).print_r($_FILES, true);
    $fp = fopen('temp.txt', 'w');
    fwrite($fp, $output);
    fclose($fp);
    echo nl2br($output);
    ?>
    After running java code, the temp.txt file on the server should contain information about posted variables and files.
    The application is working like a charm on localhost, but not with my server over the net. Please, help me solving this issue. I'm attaching source code of ClientHttpRequest java class that I'm using to make post requests.

    UPDATE
    I've just found another example of http post from java: http://stackoverflow.com/questions/1...602982#1602982
    It seams for me much more understandable, but again, it's working only with localhost server, and trying to post to the net gives me the same exception message (the full path of post request, ex. "http://domain.com/upload.php"). The app is doing something with the request for sure, because it's taking couple of seconds before exception occurs (if I set invalid host, I'm getting exception immediately). Maybe the problem is connected with some kind of response timeout? I'm still learning this programming language, so any help could be great :-)
    Attached Files Attached Files
    Last edited by Darrarski; 06-05-2010 at 01:05 AM. Reason: update

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Have you tried the GET method? If not, google it and try it out because it is more lucid than the POST method.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    Darrarski is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    I didn't try GET because I need to upload a file. As far as I know, GET won't work for sending files. I have already working code, but the problem is that I can't use it to send data over the internet. It's working only with my local server (MAMP) installed on my computer.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    working only with my local webserver
    URL("http://localhost/upload.php")
    Are you using the URL shown here? You need to change it to the server you want to talk to.
    localhost is same as 127.0.0.1

  5. #5
    Darrarski is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    @Norm, sorry, but I'm not stupid. I have couple servers with valid domains and tested several combinations. The code I've pasted was only for example, to show what technique I'm using to make POST request from JAVA. It's working only when I'm posting to localhost. I'm PHP developer, just started to learn JAVA. If you have more experience with JAVA, please try my code yourself. You can even use my server to test if this is working.
    http://ppz.darrarski.pl/upload.php
    If you POST to this address you should get two arrays (in plain text) as a response: one for posted variables, second for posted files. If you just open this link in your browser you will see empty arrays printed out.
    This example code that I'm using:
    http://stackoverflow.com/questions/1...602982#1602982
    printing server response to the System.out so you will be able to check what was received by the server after your post.
    Last edited by Darrarski; 06-05-2010 at 03:33 AM. Reason: typo

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    This is the java code I'm using
    Could you post code with a main() that executes to demo the problem?

    I'm getting exception, where ex.getMessage()
    Could you copy and paste full text of the error message here. Change the code to have it do a printStackTrace() vs just extracting the text of the message.
    Last edited by Norm; 06-05-2010 at 03:56 AM.

  7. #7
    Darrarski is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    No problem. I'm attaching a simple project (made with NetBeans) used to test POST request.
    You have to change in jButton1ActionPerformed method of test6view class:
    "path/to/some/file.jpg" to path for some file on your computer
    "http://localhost/upload.php" to valid POST request address

    For me it's working only with local server, when I'm trying to use my other server on the internet, I'm getting exception with message equals request address. Please, check my code, maybe I'm doing something wrong.
    Attached Files Attached Files

  8. #8
    Darrarski is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    I've changed the code to have a printStackTrace() when exception occurs, and this is what I've got:
    Java Code:
    java.io.FileNotFoundException: http://ppz.darrarski.pl/upload.php
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1311)
            at test6.UploadHelper.upload(UploadHelper.java:69)
            (...)
    This looks like the app can't connect with my server. It doesn't mean that it can't find the host, because when I set invalid host address, I'm getting "java.net.UnknownHostException" instead. This is strange, but the FileNotFoundException occurs whatever I put after domain name ("upload.php" in this example). When I'm testing my app with local server, everything works fine - I'm not getting any exception, files are POSTED to my local server without a problem. Could you help me?

    UPDATE
    I've checked the app with another server, and now I'm getting this:
    Java Code:
    java.io.IOException: Error writing request body to server
            at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:2645)
            at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:2628)
            at java.io.DataOutputStream.write(DataOutputStream.java:90)
            at java.io.FilterOutputStream.write(FilterOutputStream.java:80)
            at test6.UploadHelper.upload(UploadHelper.java:56)
            (...)
    Why this code is working only with localhost ?! Is there any working solution for making POST request (sending files like they would be sent from HTML form) from JAVA applications without a problem???
    Last edited by Darrarski; 06-05-2010 at 12:58 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    FileNotFoundException
    That would be from your server return a code of 404(?) Why/when does it do that?

    I have a Java program that uses Sockets to read from URLs. Here's what I get when I use that URL: java.io.FileNotFoundException: http://ppz.darrarski.pl/upload.php
    It's doing a GET.
    I also get a 404 when I put that URL in my browser: Chrome 5.0

    Can you compare the data sent to the server by the program that is not working with the data that does work? Can you write all the data the server receives to a log file for both cases and compare them?

    I'm going to work on get the different data for the above using my test server.
    More later.

  10. #10
    Darrarski is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    You are getting 404 because I've accidently moved the file when testing app. The file is back on its place now. So, you can see that you'll get java.io.FileNotFoundException: http://ppz.darrarski.pl/upload.php in my app, but Chrome and any other webbrowser should display something like that:
    Java Code:
    Array
    (
    )
    Array
    (
    )
    First array is php's $_POST, there should be variables sent to this page by POST if you sent something, second is php's $_FILES which should contain information about files POSTed to this page. The PHP script is working well, because I'm getting back correct information when testing with my local server.

    I've found another way to make POST request from JAVA, but it requires some additional libraries. Anyway, it's working with my localhost, but not with my server (ppz.darrarski.pl). I've prepare simple app, that shows me sending html post is working for one server, but not working for other. For some servers I'm getting response (just html code, that would be send to the browser), for others I'm getting only "Not Found". It's strange and I don't understand what is going on at all. Here is a link to this app with sources (cause it's to big to attach it to the post):
    http://ppz.darrarski.pl/test7.zip
    ZIP contains whole NetBeans project directory. The app has nice GUI to test it with different servers and files. If you put a valid domain in URL field, like http://google.com, you should get html as a response, and it's working with google, but not with my server. Please, check it and maybe you find the reason why this is not working.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Thanks for the downloads. However I don't use that IDE and can't do anything with them.
    I wrote a simple test pgm
    Java Code:
      //**********************************************************************
      // Following for testing
    
      public static void main(String[] args) {
       String host = "localhost:8081";
    //   host = "ppz.darrarski.pl";  
       try {
          InputStream serverInput = ClientHttpRequest.post(
          	new java.net.URL("http://" + host + "/upload.php"),
          	new Object[] {"test1", "value1", "file", new File("ClientHttpRequest.java") }
          );
          System.out.println("got " + serverInput);
       }catch(Exception x) {
         x.printStackTrace();
       }
      } // end main()
    I still get: java.io.FileNotFoundException: http://ppz.darrarski.pl/upload.php
    when I use my Socket program, but I get
    Array<br />
    (<br />
    )<br />
    Array<br />
    (<br />
    )<br />
    in Chrome???

    Why does your server do differently with a Java Socket?
    Can you see what your server gets? What is the difference between input from browser and the Java program?

    Do you have something the will send a POST to your server and get the correct response?
    Last edited by Norm; 06-05-2010 at 06:53 PM.

Similar Threads

  1. Invocation of Restful services thru POST request
    By userj2ee in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2010, 12:40 PM
  2. HTTP Post
    By erics44 in forum New To Java
    Replies: 0
    Last Post: 03-22-2010, 02:01 PM
  3. No location on http post
    By malandro95 in forum Advanced Java
    Replies: 2
    Last Post: 03-09-2010, 03:49 PM
  4. First post as per request
    By happyknappy in forum Introductions
    Replies: 3
    Last Post: 07-30-2008, 02:33 AM
  5. Replies: 1
    Last Post: 07-14-2007, 06:15 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
  •