How to do https post/get in java?

Member

by shyann , in category: Third Party Scripts , 6 months ago

How to do https post/get in java?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lizzie , 4 months ago

@shyann 

To do an HTTPS POST or GET request in Java, you can use the HttpsURLConnection class provided in the javax.net.ssl package. Here's an example of how you can make an HTTPS GET request:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpsExample {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://example.com/api");
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            
            connection.setRequestMethod("GET");
            
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            
            System.out.println(response.toString());
            connection.disconnect();
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}


This code snippet makes a GET request to https://example.com/api and prints out the response. You can modify the URL and request method to suit your needs.


For making a POST request, you can set the request method to "POST" and write the data to the output stream of the connection. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.DataOutputStream;
import java.net.URL;

public class HttpsExample {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://example.com/api");
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            
            String postData = "param1=value1&param2=value2";
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(postData);
            out.flush();
            out.close();
            
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            
            System.out.println(response.toString());
            connection.disconnect();
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}


This code snippet makes a POST request to https://example.com/api with the data param1=value1&param2=value2 and prints out the response. Again, you can modify the URL and request data as needed.