• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Samples | Rest Client HTTP (HttpURLConnection)
  1. Notes
  2. Maven dependencies
  3. GET Method
  4. POST Method (url parameters)
  5. POST Method (form parameters)

  1. Notes
    See this page for an example of a REST application: JAX-RS: Sample Application (Jersey, Spring, Tomcat)
  2. Maven dependencies
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1.1</version>
    </dependency>
  3. GET Method
    • Java example:

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.util.List;
      import java.util.Map;
      
      import javax.ws.rs.HttpMethod;
      import javax.ws.rs.core.MediaType;
      
      public class Test {
          public static void main(String[] args) throws IOException {
              final String urlString = "http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/getMyEntity1?qpvalue1=val11&qpvalue2=val21&qpvalue3=val31";
      
              final int connectTimeout = 1000;
      
              HttpURLConnection connection = null;
      
              try {
                  final URL url = new URL(urlString);
      
                  connection = (HttpURLConnection) url.openConnection();
      
                  connection.setConnectTimeout(connectTimeout);
      
                  connection.setRequestMethod(HttpMethod.GET);
      
                  connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
      
                  // response code
                  System.out.println(connection.getResponseCode());
      
                  // response headers
                  final Map<String, List<String>> headerFields = connection.getHeaderFields();
                  for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
                      System.out.println("Header [Key: " + entry.getKey() + " - Value: " + entry.getValue() + "]");
                  }
      
                  // response body
                  if (connection.getInputStream() != null) {
                      final InputStream inputStream = connection.getInputStream();
                      final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
      
                      try {
                          final StringBuilder response = new StringBuilder();
      
                          String line;
                          while ((line = bufferedReader.readLine()) != null) {
                              response.append(line);
                          }
      
                          System.out.println(response.toString());
                      } finally {
                          if (bufferedReader != null) {
                              bufferedReader.close();
                          }
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  if (connection != null) {
                      connection.disconnect();
                  }
              }
          }
      }

    • Sample JAX-RS GET resource:

      @GET
      @Path("/getMyEntity1")
      public Response getMyEntity1(@QueryParam(value = "qpvalue1") final String value1,
              @QueryParam(value = "qpvalue2") final String value2,
              @QueryParam(value = "qpvalue3") final String value3) {
          MyEntity myEntity = new MyEntity();
      
          //...
      
          return Response.ok(myEntity).build();
      }

    • Request/Response:

      200
      Header [Key: null - Value: [HTTP/1.1 200]]
      Header [Key: Content-Length - Value: [688]]
      Header [Key: Date - Value: [Mon, 21 Oct 2018 00:45:54 GMT]]
      Header [Key: Content-Type - Value: [application/xml]]
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?><myEntity ...

  4. POST Method (url parameters)
    • Java example:

      import java.io.BufferedReader;
      import java.io.DataOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.OutputStream;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.util.List;
      import java.util.Map;
      
      import javax.ws.rs.HttpMethod;
      import javax.ws.rs.core.MediaType;
      
      public class Test {
          public static void main(String[] args) throws IOException {
              final String urlString = "http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/postMyEntity1?qpvalue1=val11&qpvalue2=val21&qpvalue3=val31";
      
              final int connectTimeout = 1000;
      
              HttpURLConnection connection = null;
      
              try {
                  final URL url = new URL(urlString);
      
                  connection = (HttpURLConnection) url.openConnection();
      
                  connection.setConnectTimeout(connectTimeout);
      
                  connection.setRequestMethod(HttpMethod.POST);
      
                  connection.setRequestProperty("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
                  connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
      
                  connection.setUseCaches(false);
                  connection.setDoOutput(true);
      
                  final OutputStream outputStream = connection.getOutputStream();
      
                  final DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                  dataOutputStream.close();
      
                  // response code
                  System.out.println(connection.getResponseCode());
      
                  // response headers
                  final Map<String, List<String>> headerFields = connection.getHeaderFields();
                  for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
                      System.out.println("Header [Key: " + entry.getKey() + " - Value: " + entry.getValue() + "]");
                  }
      
                  // response body
                  if (connection.getInputStream() != null) {
                      final InputStream inputStream = connection.getInputStream();
      
                      final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
      
                      try {
                          StringBuilder response = new StringBuilder();
      
                          String line;
                          while ((line = bufferedReader.readLine()) != null) {
                              response.append(line);
                          }
      
                          System.out.println(response.toString());
                      } finally {
                          if (bufferedReader != null) {
                              bufferedReader.close();
                          }
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  if (connection != null) {
                      connection.disconnect();
                  }
              }
          }
      }

    • Sample JAX-RS POST resource:

      @POST
      @Path("/postMyEntity1")
      public Response postMyEntity1(@QueryParam(value = "qpvalue1") final String value1,
              @QueryParam(value = "qpvalue2") final String value2,
              @QueryParam(value = "qpvalue3") final String value3) {
          MyEntity myEntity = new MyEntity();
      
          //...
      
          return Response.ok(myEntity).build();
      }

    • Request/Response:

      200
      Header [Key: null - Value: [HTTP/1.1 200]]
      Header [Key: Content-Length - Value: [996]]
      Header [Key: Date - Value: [Mon, 21 Oct 2018 00:48:39 GMT]]
      Header [Key: Content-Type - Value: [application/xml]]
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?><myEntity ...

  5. POST Method (form parameters)
    • Java example:

      import java.io.BufferedReader;
      import java.io.DataOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.OutputStream;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.util.List;
      import java.util.Map;
      
      import javax.ws.rs.HttpMethod;
      import javax.ws.rs.core.MediaType;
      
      public class Test {
          public static void main(String[] args) throws IOException {
              final String urlString = "http://localhost:8080/mtitek-rest-sample-1.0.0-SNAPSHOT/rest/myEntity/postMyEntity2";
              final String formParameters = "qpvalue1=val11&qpvalue2=val21&qpvalue3=val31";
      
              final int connectTimeout = 1000;
      
              HttpURLConnection connection = null;
      
              try {
                  final URL url = new URL(urlString);
      
                  connection = (HttpURLConnection) url.openConnection();
      
                  connection.setConnectTimeout(connectTimeout);
      
                  connection.setRequestMethod(HttpMethod.POST);
      
                  connection.setRequestProperty("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
                  connection.setRequestProperty("Accept", MediaType.APPLICATION_XML);
      
                  connection.setUseCaches(false);
                  connection.setDoOutput(true);
      
                  final OutputStream outputStream = connection.getOutputStream();
      
                  final DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                  dataOutputStream.writeBytes(formParameters);
                  dataOutputStream.close();
      
                  // response code
                  System.out.println(connection.getResponseCode());
      
                  // response headers
                  final Map<String, List<String>> headerFields = connection.getHeaderFields();
                  for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
                      System.out.println("Header [Key: " + entry.getKey() + " - Value: " + entry.getValue() + "]");
                  }
      
                  // response body
                  if (connection.getInputStream() != null) {
                      final InputStream inputStream = connection.getInputStream();
      
                      final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
      
                      try {
                          StringBuilder response = new StringBuilder();
      
                          String line;
                          while ((line = bufferedReader.readLine()) != null) {
                              response.append(line);
                          }
      
                          System.out.println(response.toString());
                      } finally {
                          if (bufferedReader != null) {
                              bufferedReader.close();
                          }
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  if (connection != null) {
                      connection.disconnect();
                  }
              }
          }
      }

    • Sample JAX-RS POST resource:

      @POST
      @Path("/postMyEntity2")
      @Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
      public Response postMyEntity2(@FormParam(value = "qpvalue1") final String value1,
              @FormParam(value = "qpvalue2") final String value2,
              @FormParam(value = "qpvalue3") final String value3) {
          MyEntity myEntity = new MyEntity();
      
          //...
      
          return Response.ok(myEntity).build();
      }

    • Request/Response:

      200
      Header [Key: null - Value: [HTTP/1.1 200]]
      Header [Key: Content-Length - Value: [934]]
      Header [Key: Date - Value: [Mon, 21 Oct 2018 00:49:58 GMT]]
      Header [Key: Content-Type - Value: [application/xml]]
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?><myEntity ...
© 2025  mtitek