title: HTTP Request in Java - Simple Implementation
body: <p>I am totally new in Java. I Have just downloaded Eclipse and the latest JDK.</p>
  
  <p>Now I want to perform <strong>REST based HTTP requests in a Java application</strong> built specifically for this http requests, almost like a 'hello world' type application. I came across the following tutorials on how to do it, but I am not sure how to actually implement it in Eclipse as a project and as a working Java Application.</p>
  
  <p><a href="http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/" rel="nofollow noreferrer">http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/</a></p>
  
  <p><a href="http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139" rel="nofollow noreferrer">http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139</a></p>
  
  <hr>
  
  <p><strong>Now, what I did is the following:</strong></p>
  
  <p>I took the code form the Second Link example and I saved it into a file called: </p>
  
  <pre><code>HttpURLConnectionExample.java
  </code></pre>
  
  <p>Here is the code inside it:</p>
  
  <pre><code>package com.mkyong;
  
  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;
  
  public class HttpURLConnectionExample {
  
      private final String USER_AGENT = "Mozilla/5.0";
  
      public static void main(String[] args) throws Exception {
  
          HttpURLConnectionExample http = new HttpURLConnectionExample();
  
          System.out.println("Testing 1 - Send Http GET request");
          http.sendGet();
  
          System.out.println("\nTesting 2 - Send Http POST request");
          http.sendPost();
  
      }
  
      // HTTP GET request
      private void sendGet() throws Exception {
  
          String url = "http://www.google.com/search?q=mkyong";
  
          URL obj = new URL(url);
          HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  
          // optional default is GET
          con.setRequestMethod("GET");
  
          //add request header
          con.setRequestProperty("User-Agent", USER_AGENT);
  
          int responseCode = con.getResponseCode();
          System.out.println("\nSending 'GET' request to URL : " + url);
          System.out.println("Response Code : " + responseCode);
  
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(con.getInputStream()));
          String inputLine;
          StringBuffer response = new StringBuffer();
  
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
          }
          in.close();
  
          //print result
          System.out.println(response.toString());
  
      }
  
      // HTTP POST request
      private void sendPost() throws Exception {
  
          String url = "https://selfsolve.apple.com/wcResults.do";
          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");
  
          String urlParameters = "sn=C02G8416DRJM&amp;cn=&amp;locale=&amp;caller=&amp;num=12345";
  
          // Send post request
          con.setDoOutput(true);
          DataOutputStream wr = new DataOutputStream(con.getOutputStream());
          wr.writeBytes(urlParameters);
          wr.flush();
          wr.close();
  
          int responseCode = con.getResponseCode();
          System.out.println("\nSending 'POST' request to URL : " + url);
          System.out.println("Post parameters : " + urlParameters);
          System.out.println("Response Code : " + responseCode);
  
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(con.getInputStream()));
          String inputLine;
          StringBuffer response = new StringBuffer();
  
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
          }
          in.close();
  
          //print result
          System.out.println(response.toString());
  
      }
  
  }
  </code></pre>
  
  <p>and I imported it into Eclipse: <strong>File>Open File</strong>. So now I right-clicked on the class (HttpURLConnectionExample.java) in the right hand side 'Outline' Window and under <strong>'Run Configurations'</strong> I specified a 'project' for it, called <strong>http_request</strong>.</p>
  
  <p><img src="https://i.stack.imgur.com/rhaLg.png" alt="enter image description here"></p>
  
  <p>Now, when I right-click on the left-hand side window on 'http_requests', I go: run as>java application. It gives me the following error:</p>
  
  <pre><code>Launch Error: Selection does not contain a main type
  </code></pre>
  
  <p><strong>My question:</strong> What <strong>main type</strong> do I need to specify for it? How do I actually execute the http requests as seen in the code posted above? How do I run this?</p>