title: Get and Post API call in java with basic authentication
body: <p>I want to call <code>GET</code> and <code>POST</code> API in <code>java</code> without using any <code>framework</code>. I need to use basic authentication. Can anybody help me with some tutorial link. In google I found code only in <code>spring</code> framework, But I am not using <code>Spring</code>. I am looking for code to call API with <code>basic authentication.</code></p>
  <p>I have to add new <code>url</code> with <code>authentication</code> in the below <code>code</code>. What modification is required if <code>API</code> is secured with <code>basic auth</code> and it is <code>POST</code> method. I am new to <code>java</code> so not much aware.</p>
  <pre><code>import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.Proxy;
  import java.net.URL;
  import java.net.URLConnection;
  
  public class NetClientGet {
  
      public static void main(String[] args)  {
          
          try
          {
              System.out.println(&quot;Inside the main function&quot;);
               URL weburl=new URL(&quot;http://dummy.restapiexample.com/api/v1/employees&quot;);
               HttpURLConnection conn = (HttpURLConnection) weburl.openConnection();
               conn.setRequestMethod(&quot;GET&quot;);
               conn.setRequestProperty(&quot;Accept&quot;, &quot;application/json&quot;);
               System.out.println(&quot;Output is: &quot;+conn.getResponseCode());
               System.out.println(&quot;Output is: &quot;);
               System.setProperty(&quot;http.proxyHost&quot;, null);
               //conn.setConnectTimeout(60000);
               if(conn.getResponseCode()!=200)
               {
                   System.out.println(conn.getResponseCode());
                   throw new RuntimeException(&quot;Failed : HTTP Error Code: &quot;+conn.getResponseCode());
               }
               System.out.println(&quot;After the 2 call &quot;);
               InputStreamReader in=new InputStreamReader(conn.getInputStream());
               BufferedReader br =new BufferedReader(in);
               String output;
               while((output=br.readLine())!=null)
               {
                   System.out.println(output);
               }
               conn.disconnect();
               
          }
          catch(Exception e)
          {
              System.out.println(e.getMessage());
          }
          
      }
  }
  </code></pre>