Results 1 to 7 of 7
- 06-22-2011, 03:04 PM #1
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Sending a file to a web page using POST method
Hi Advanced users,
I have an application and I would like it to send a file to a web page using POST method.
I've found the HttpClient from Apache, but I had no luck. All the tutorials and samples I can find on the net are for 3.x. I've tried adopting the samples with the HttpClient 4.1 docs, but I failed.
I have a target url of a page and source file path. I get the basic idea of what needs to happen, I am just finding it hard to implement it.
Any help would be appreciated, Thanks.Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 06-22-2011, 03:27 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
Some code might help...along with what you mean by "I had no luck".
- 06-22-2011, 03:30 PM #3
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
I keep getting 406 Not Acceptable Response.Java Code:File file = new File("target file location"); String targetURL = "URL to a .php web page"; HttpClient httpclient = new DefaultHttpClient(); try { HttpPost httppost = new HttpPost(targetURL); InputStreamEntity reqEntity = new InputStreamEntity( new FileInputStream(file), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); // It may be more appropriate to use FileEntity class in this particular // instance but we are using a more generic InputStreamEntity to demonstrate // the capability to stream out data from any arbitrary source // // FileEntity entity = new FileEntity(file, "binary/octet-stream"); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println("Response content length: " + resEntity.getContentLength()); System.out.println("Chunked?: " + resEntity.isChunked()); } EntityUtils.consume(resEntity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
EDIT: This is essentially one of the examples from http://hc.apache.org/httpcomponents-.../examples.htmlLast edited by FlyNn; 06-22-2011 at 03:33 PM.
Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 06-22-2011, 03:38 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
Which means the server is trying to return something the client has told it it can't accept.
What data is the server trying to return?
- 06-22-2011, 03:54 PM #5
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
That's a very good questions cause I did not write the .php page on the server side. I do know it processes at the file and echos the contents out.
Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 06-22-2011, 03:58 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
You should be able to find out what the Accept: header is for your post, though I couldn't tell you exactly how.
Then you'll need to find a way of seeing what the server is planning on sending.
If it's echoing your file back then I suspect it may be whatever MIME type that would fall under.
- 06-22-2011, 05:16 PM #7
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
I have changed the code like this:
I have added the fileEntity and setting it for the httppost and correctly executing it.Java Code:HttpClient httpclient = new DefaultHttpClient(); try { HttpPost httppost = new HttpPost(targetURL); FileEntity fe = new FileEntity(file,"text/xml; charset=\"UTF-8\""); httppost.setEntity(fe); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println("Response content length: " + resEntity.getContentLength()); System.out.println("Chunked?: " + resEntity.isChunked()); } EntityUtils.consume(resEntity); }catch(Exception e){ e.printStackTrace(); } finally { httpclient.getConnectionManager().shutdown(); }
I get 200 OK response.
Thank youMeasuring programming progress by lines of code is like measuring aircraft building progress by weight.
Similar Threads
-
Java applet post get method to php
By lotdox in forum Java AppletsReplies: 0Last Post: 01-07-2011, 12:17 AM -
Passing parameter from JApplet to php file using POST method
By mneskovic in forum New To JavaReplies: 10Last Post: 08-05-2010, 05:27 PM -
Sending text to a web page
By shyameni in forum Advanced JavaReplies: 2Last Post: 10-08-2009, 07:14 AM -
Sending text to a web page
By shyameni in forum Advanced JavaReplies: 0Last Post: 10-07-2009, 06:38 PM -
Post Method in java.net
By freddieMaize in forum Advanced JavaReplies: 2Last Post: 02-23-2009, 02:59 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks