1 Attachment(s)
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:
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:
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 :-)