检查 URL 是否有效或在 java 中存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10551813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:32:08  来源:igfitidea点击:

Check if URL is valid or exists in java

javahttpurl

提问by Podge

I'm writing a Java program which hits a list of urls and needs to first know if the url exists. I don't know how to go about this and cant find the java code to use.

我正在编写一个 Java 程序,它访问一个 url 列表,需要首先知道 url 是否存在。我不知道该怎么做,也找不到要使用的 java 代码。

The URL is like this:

网址是这样的:

http: //ip:port/URI?Attribute=x&attribute2=y

These are URLs on our internal network that would return an XML if valid.

这些是我们内部网络上的 URL,如果有效,将返回 XML。

Can anyone suggest some code?

任何人都可以建议一些代码吗?

回答by sealz

You could just use httpURLConnection. If it is not valid you won't get anything back.

你可以只使用httpURLConnection. 如果它无效,您将不会得到任何回报。

    HttpURLConnection connection = null;
try{         
    URL myurl = new URL("http://www.myURL.com");        
    connection = (HttpURLConnection) myurl.openConnection(); 
    //Set request to header to reduce load as Subirkumarsao said.       
    connection.setRequestMethod("HEAD");         
    int code = connection.getResponseCode();        
    System.out.println("" + code); 
} catch {
//Handle invalid URL
}

Or you could ping it like you would from CMD and record the response.

或者你可以像从 CMD 一样 ping 它并记录响应。

String myurl = "google.com"
String ping = "ping " + myurl 

try {
    Runtime r = Runtime.getRuntime();
    Process p = r.exec(ping);
    r.exec(ping);    
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String inLine;
    BufferedWriter write = new BufferedWriter(new FileWriter("C:\myfile.txt"));

    while ((inLine = in.readLine()) != null) {             
            write.write(inLine);   
            write.newLine();
        }
            write.flush();
            write.close();
            in.close();              
     } catch (Exception ex) {
       //Code here for what you want to do with invalid URLs
     } 
}

回答by Subir Kumar Sao

  1. A malformed url will give you an exception.
  2. To know if you the url is active or not you have to hit the url. There is no other way.
  1. 格式错误的网址会给您一个例外。
  2. 要知道您的网址是否处于活动状态,您必须点击该网址。没有其他办法。

You can reduce the load by requesting for a header from the url.

您可以通过从 url 请求标头来减少负载。

回答by Kumar Vivek Mitra

package com.my;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

public class StrTest {

public static void main(String[] args) throws IOException {

    try {
        URL url = new URL("http://www.yaoo.coi");
        InputStream i = null;

        try {
            i = url.openStream();
        } catch (UnknownHostException ex) {
            System.out.println("THIS URL IS NOT VALID");
        }

        if (i != null) {
            System.out.println("Its working");
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
           }
      }
  }

output : THIS URL IS NOT VALID

输出:此 URL 无效

回答by jarnbjo

Open a connection and check if the response contains valid XML? Was that too obvious or do you look for some other magic?

打开连接并检查响应是否包含有效的 XML?这太明显了还是你在寻找其他魔法?

回答by Juan Alberto López Cavallotti

You may want to use HttpURLConnection and check for error status:

您可能想要使用 HttpURLConnection 并检查错误状态:

HttpURLConnection javadoc

HttpURLConnection javadoc