如何解决HttpURLConnection中的java.net.UnknownHostException

时间:2017-10-18 10:10:36

标签: java url httpurlconnection urlconnection

我正在尝试在远程服务器上实施SMS电话号码验证程序。下面的方法由Servlet调用,我试图连接到SMS服务提供商,以便他们可以发送短信到指定的电话号码:

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;
    }
}

执行servlet时,我得到以下异常:

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

当我尝试使用以下代码实例化URL时:

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

我得到javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

如果我将https更改为http,我会将301作为状态代码。 但是,如果我在Web浏览器上运行URL地址,我会得到: enter image description here

...我也收到了预期的短信!

我还检查了hereherehere,我似乎从这些帖子中得不到任何内容。 我不知道我在这里做错了什么。 请帮忙。

2 个答案:

答案 0 :(得分:0)

那是因为https不使用端口80.它使用端口443代替,尝试以下方法:

{{1}}

答案 1 :(得分:-1)

您网站的HTTP纯文本版本向www.发出了无效的重定向。当您使用HTTPS时,它会先失败,因为您正在连接到错误的HTTPS端口。