code_junkie

how to (simply) generate POST http request from java to do the file upload

Nov 12th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. public void post(String url) throws Exception {
  2. URL u = new URL(url);
  3. URLConnection c = u.openConnection();
  4.  
  5. c.setDoOutput(true);
  6. if (c instanceof HttpURLConnection) {
  7. ((HttpURLConnection)c).setRequestMethod("POST");
  8. }
  9.  
  10. OutputStreamWriter out = new OutputStreamWriter(
  11. c.getOutputStream());
  12.  
  13. // output your data here
  14.  
  15. out.close();
  16.  
  17. BufferedReader in = new BufferedReader(
  18. new InputStreamReader(
  19. c.getInputStream()));
  20.  
  21. String s = null;
  22. while ((s = in.readLine()) != null) {
  23. System.out.println(s);
  24. }
  25. in.close();
  26. }
Add Comment
Please, Sign In to add comment