Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import javax.net.ssl.HttpsURLConnection;
- /**
- * Http Function class
- *
- * @author grunclepug
- * @version hopefully the only one
- */
- public class HttpFunctions
- {
- //Constants
- private final String USER_AGENT = "Mozilla/5.0";
- //Variables
- private String url;
- public HttpFunctions(boolean registerFlag, boolean loginFlag, String urlParameters) throws Exception
- {
- if(registerFlag)
- {
- url = "https://summer2019.garytong.ca/register";
- }
- if(loginFlag)
- {
- url = "https://summer2019.garytong.ca/login";
- }
- sendPost(urlParameters);
- }
- // HTTP POST request
- private void sendPost(String urlParameters) throws Exception {
- URL obj = new URL(url);
- HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
- //add request header
- con.setRequestMethod("POST");
- con.setRequestProperty("User-Agent", USER_AGENT);
- con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
- // Send post request
- con.setDoOutput(true);
- DataOutputStream wr = new DataOutputStream(con.getOutputStream());
- wr.writeBytes(urlParameters);
- wr.flush();
- wr.close();
- int responseCode = con.getResponseCode();
- if(responseCode == 202)
- {
- System.out.println("User registered!");
- }
- else if(responseCode == 200)
- {
- System.out.println("User logged in!");
- }
- else
- {
- System.out.println("Invalid input.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement