本文介绍了java.net.UnknownHostException:www.google.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在开发健全性检查Web应用程序。我尝试使用HttpUrlConnection方法获取url响应,但出现UnknownHostException。

I am developing a sanity check web application. I tried getting url response using HttpUrlConnection method but I am getting UnknownHostException.

 System.setProperty("java.net.preferIPv4Stack" , "true");
    String[] uat_targetUrls={"https://www.google.com"};
    String[] uat_targetResponse=new String[uat_targetUrls.length];

            HttpURLConnection httpUrlConn;
            httpUrlConn = (HttpURLConnection) new URL(uat_targetUrls[i])
            .openConnection();

            httpUrlConn.setRequestMethod("GET");


            httpUrlConn.setConnectTimeout(30000);
            httpUrlConn.setReadTimeout(30000);



           if(httpUrlConn.getResponseCode()==200)
               uat_targetResponse[i]="UP";
           else
               uat_targetResponse[i]="DOWN";

执行此操作时,我得到各种URL的UnknownHostException。谁可以帮我这个事。我正在使用Eclipse IDE。这是我得到的错误:

When executing this, I am getting UnknownHostException for various urls. Can anyone help me on this. I am using Eclipse IDE. This is the error I am getting:

java.net.UnknownHostException: www.google.com
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)

谢谢。

推荐答案

问题必定是您计算机上的网络问题。

The problem must be a networking issue on your machine.

您的代码对我有用(有一些小的修复以修复丢失的循环变量 i ):

Your code works for me (with some minor fixes to repair the missing loop variable i):

public static void main(String[] args) throws Exception {
    System.setProperty("java.net.preferIPv4Stack", "true");
    String[] uat_targetUrls = { "https://www.google.com" };
    String[] uat_targetResponse = new String[uat_targetUrls.length];

    HttpURLConnection httpUrlConn;
    httpUrlConn = (HttpURLConnection) new URL(uat_targetUrls[0])
            .openConnection();

    httpUrlConn.setRequestMethod("GET");

    httpUrlConn.setConnectTimeout(30000);
    httpUrlConn.setReadTimeout(30000);

    if (httpUrlConn.getResponseCode() == 200)
        uat_targetResponse[0] = "UP";
    else
        uat_targetResponse[0] = "DOWN";


    System.out.println(uat_targetResponse[0]);
}

输出: UP

这篇关于java.net.UnknownHostException:www.google.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 16:04