Java Post Requests Class


/ Published in: Java
Save to your folder(s)

This is a class i put together to handle POST requests


Copy this code and paste it in your HTML
  1. import java.io.*;
  2. import java.net.*;
  3. import javax.swing.*;
  4.  
  5. public class Requests {
  6. static String domain;
  7.  
  8.  
  9. static URL myurl;
  10. static URLConnection myurlc;
  11. static OutputStreamWriter osr;
  12. static BufferedReader br;
  13. static String line;
  14. static String resp;
  15. static StringBuilder sb;
  16.  
  17.  
  18. public Requests(String d) {
  19. domain = d;
  20. }
  21.  
  22. public String sendPost(String data) {
  23. try {
  24. sb = new StringBuilder();
  25. myurl = new URL(domain);
  26. myurlc = myurl.openConnection();
  27. myurlc.setDoOutput(true);
  28. osr = new OutputStreamWriter(myurlc.getOutputStream());
  29. osr.write(data);
  30. osr.flush();
  31.  
  32. br = new BufferedReader(new InputStreamReader(myurlc.getInputStream()));
  33. while((line = br.readLine()) != null)
  34. {
  35. sb.append(line);
  36. }
  37. br.close();
  38. osr.close();
  39. }
  40. catch(IOException e)
  41. {
  42. JOptionPane.showMessageDialog(null,"IO Error");
  43. }
  44. finally {
  45. return sb.toString();
  46. }
  47. }
  48. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.