1

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:

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&from=Matonia&to=" + phoneNumber + "&body=" + message;
    URL url = new URL(address);
    //URL url = new URL("https", "www.bulksmsnigeria.com", 80, "/api/v1/sms/create?api_token=fHTGT6PiRxIE1ZrMvOTDcsu0cM5yiQKz2EnWAXxQqkMt6UlAUHwHGIBFxQW4&from=Matonia&to=" + phoneNumber + "&body=" + message);
    HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
    urlConn.setRequestMethod("POST");
    try(InputStream inputStream = urlConn.getInputStream())//<- 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;
    }
}

When the servlet is executed, I get the following exception:

 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.<init>(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 ...

When I tried using the following code to instantiate URL:

URL url = new URL("https", "www.bulksmsnigeria.com", 80, "/api/v1/sms/create?api_token=fHTGT6PiRxIE1ZrMvOTDcsu0cM5yiQKz2EnWAXxQqkMt6UlAUHwHGIBFxQW4&from=Matonia&to=" + phoneNumber + "&body=" + message);

I get javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

If I change the https to http, I get 301 as status code. However if I run the URL address on the web browser I get this: enter image description here

...and I also received the expected SMS message!

I have also checked here, here, and here and I don't seem to get anything from those posts. I don't know what I am doing wrong here. Please help.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jevison7x
  • 709
  • 2
  • 14
  • 31

2 Answers2

1

That's because https does not use port 80. It uses port 443 instead, try the following:

URL url = new URL("https", "www.bulksmsnigeria.com", 443, 
    "/api/v1/sms/create?api_token=fHTGT6PiRxIE1ZrMvOTDcsu0cM5yiQKz2EnWAXxQqkMt6UlAUHwHGIBFxQW4&from=Matonia&to=" + phoneNumber + "&body=" + message;
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Let me try that... – Jevison7x Oct 18 '17 at 10:24
  • I am still getting `java.net.UnknownHostException: www.` – Jevison7x Oct 18 '17 at 10:48
  • Are you sure the server is connected to internet? Can you try with any other url (e.g. `https://www.google.com`)? – Darshan Mehta Oct 18 '17 at 11:11
  • The server I am running the code is a remote server, so I am sure it is connected to the internet. However, I've used that code with another host before (although it wasn't HTTPS) and it worked. – Jevison7x Oct 18 '17 at 11:22
  • Could you post the code with actual value of message and phone number so that I can run it as is? So far, I haven't been able to re-produce the bug. – Darshan Mehta Oct 18 '17 at 11:29
  • You can use any message and phone number of your choice @Darshan Mehta, please see if you can help. Thanks. – Jevison7x Oct 18 '17 at 12:29
  • Could you try `ping www.bulksmsnigeria.com` from command line of your server and see if you get replies back? Looks like proxy is not configured in your server. – Darshan Mehta Oct 18 '17 at 12:51
  • I did, I got 4 replies from 173.214.186.111. I even tried using the given IP address on the code and I got the same result. – Jevison7x Oct 18 '17 at 19:09
-1

The HTTP plaintext version of your site is issuing an invalid redirect to www.. When you used HTTPS it fails earlier because you're connecting to the wrong port for HTTPS.

user207421
  • 305,947
  • 44
  • 307
  • 483