title: How to solve java.net.UnknownHostException from HttpURLConnection
body: <p>I am trying to implement an SMS phone number verification program on a remote server. The method below is called by a Servlet, I am trying to connect to the SMS service provider so that they can send an SMS to the specified phone number:</p>
  
  <pre><code>public static int sendPhoneNumberVerication(User user) throws SQLException, IOException, IllegalArgumentException, ClassNotFoundException
  {
      String email = user.getEmail();
      String phoneNumber = user.getPhoneNumber();
      String verificationCode = getVerificationCode(email);
      String message = getPhoneVerificationMessage(user, verificationCode);
      //The sending SMS part
      String address = "https://www.bulksmsnigeria.com/api/v1/sms/create?api_token=fHTGT6PiRxIE1ZrMvOTDcsu0cM5yiQKz2EnWAXxQqkMt6UlAUHwHGIBFxQW4&amp;from=Matonia&amp;to=" + phoneNumber + "&amp;body=" + message;
      URL url = new URL(address);
      //URL url = new URL("https", "www.bulksmsnigeria.com", 80, "/api/v1/sms/create?api_token=fHTGT6PiRxIE1ZrMvOTDcsu0cM5yiQKz2EnWAXxQqkMt6UlAUHwHGIBFxQW4&amp;from=Matonia&amp;to=" + phoneNumber + "&amp;body=" + message);
      HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
      urlConn.setRequestMethod("POST");
      try(InputStream inputStream = urlConn.getInputStream())//&lt;- Exception is throw here
      {
          byte[] bytes = new byte[inputStream.available()];
          inputStream.read(bytes);
          String content = new String(bytes);
          //Get the HTTP response status code
          int statusCode = urlConn.getResponseCode();
          return statusCode;
      }
  }
  </code></pre>
  
  <p>When the servlet is executed, I get the following exception:</p>
  
  <pre><code> java.net.UnknownHostException: www.
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  at java.net.Socket.connect(Socket.java:589)
  at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:649)
  at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
  at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
  at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
  at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
  at sun.net.www.protocol.https.HttpsClient.&lt;init&gt;(HttpsClient.java:275)
  at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
  at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
  at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1103)
  at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
  at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
  at sun.net.www.protocol.http.HttpURLConnection.followRedirect0(HttpURLConnection.java:2645)
  at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:2574)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1768)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
  at com.peer2peer.users.UserDAO.sendPhoneNumberVerication(UserDAO.java:406)
  at com.peer2peer.users.UpdateProfileServlet.sendPhoneValidation(UpdateProfileServlet.java:261)
  at com.peer2peer.users.UpdateProfileServlet.processRequest(UpdateProfileServlet.java:52)
  at com.peer2peer.users.UpdateProfileServlet.doGet(UpdateProfileServlet.java:333)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
  at ...
  </code></pre>
  
  <p>When I tried using the following code to instantiate URL:</p>
  
  <pre><code>URL url = new URL("https", "www.bulksmsnigeria.com", 80, "/api/v1/sms/create?api_token=fHTGT6PiRxIE1ZrMvOTDcsu0cM5yiQKz2EnWAXxQqkMt6UlAUHwHGIBFxQW4&amp;from=Matonia&amp;to=" + phoneNumber + "&amp;body=" + message);
  </code></pre>
  
  <p>I get <code>javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?</code></p>
  
  <p>If I change the <code>https</code> to <code>http</code>, I get <code>301</code> as status code.
  However if I run the URL address on the web browser I get this: 
  <a href="https://i.stack.imgur.com/buwxJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/buwxJ.jpg" alt="enter image description here"></a></p>
  
  <p>...and I also received the expected SMS message!</p>
  
  <p>I have also checked <a href="https://stackoverflow.com/questions/43469589/httpurlconnection-post-request-not-working-how-to-solve-this">here</a>, <a href="https://stackoverflow.com/questions/6484275/java-net-unknownhostexception-invalid-hostname-for-server-local">here</a>, and <a href="https://stackoverflow.com/questions/25992586/java-httpurlconnection-java-net-unknownhostexception">here</a> and I don't seem to get anything from those posts.
  I don't know what I am doing wrong here.
  Please help.</p>