Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6.  
  7. import javax.net.ssl.HttpsURLConnection;
  8.  
  9. /**
  10. * Http Function class
  11. *
  12. * @author grunclepug
  13. * @version hopefully the only one
  14. */
  15. public class HttpFunctions
  16. {
  17. //Constants
  18. private final String USER_AGENT = "Mozilla/5.0";
  19.  
  20. //Variables
  21. private String url;
  22.  
  23. public HttpFunctions(boolean registerFlag, boolean loginFlag, String urlParameters) throws Exception
  24. {
  25. if(registerFlag)
  26. {
  27. url = "https://summer2019.garytong.ca/register";
  28. }
  29.  
  30. if(loginFlag)
  31. {
  32. url = "https://summer2019.garytong.ca/login";
  33. }
  34.  
  35. sendPost(urlParameters);
  36. }
  37.  
  38. // HTTP POST request
  39. private void sendPost(String urlParameters) throws Exception {
  40.  
  41. URL obj = new URL(url);
  42. HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  43.  
  44. //add request header
  45. con.setRequestMethod("POST");
  46. con.setRequestProperty("User-Agent", USER_AGENT);
  47. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  48.  
  49. // Send post request
  50. con.setDoOutput(true);
  51. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  52. wr.writeBytes(urlParameters);
  53. wr.flush();
  54. wr.close();
  55.  
  56. int responseCode = con.getResponseCode();
  57.  
  58. if(responseCode == 202)
  59. {
  60. System.out.println("User registered!");
  61. }
  62. else if(responseCode == 200)
  63. {
  64. System.out.println("User logged in!");
  65. }
  66. else
  67. {
  68. System.out.println("Invalid input.");
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement