JAVA中是否可以有多个URL连接?

发布于 2024-09-12 04:29:56 字数 3549 浏览 1 评论 0原文

我在使 URL 连接正常工作时遇到一些问题。我发现如果我创建一个 URL 对象,它就可以正常工作。但之后的一切根本不起作用。起初我以为这可能是因为我没有关闭流,但事实并非如此。

-1 = 失败

1 = 成功

Testing:
        URLFail test0 = new URLFail();
        URLFail test1 = new URLFail(1);
        URLFail test2 = new URLFail(1,1);

OutPut:
    0Param  1
    1Param -1
    2Param -1

现在,如果我切换先创建哪个对象,结果就会改变。

Testing:
        URLFail test2 = new URLFail(1,1);
        URLFail test0 = new URLFail();
        URLFail test1 = new URLFail(1);

OutPut:
      0Param -1
      1Param -1
      2Param  1

在本例中,只有 test2 成功。这让我相信每个类只能使用一个 URL 流?

这就是我一直用来访问和解析 URL 的方法。

        URL url = null;
        URLConnection urlConnection = null;
        try {
            url = new URL("xxxxx"); // In my program this is a proper URL

        } catch (MalformedURLException ex) {
            return -1;
        }

        ArrayList<String> index = new ArrayList<String>();

        try {
            urlConnection = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            while (in.ready()) {
                index.add(in.readLine());
            }

            in.close();
        } catch (IOException ex) {
            return -1;

        }

到目前为止我真的不知道出了什么问题。我认为这可能是因为我尝试访问同一网站的速度太快?

我还尝试过使用 URL 中的 openStream() 方法,在没有 URLConnection 对象的情况下进行操作。任何帮助将不胜感激,也欢迎任何有关代码本身的一般评论。谢谢!

这是整个代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class URLFail {
    private int name;

    public static void main(String[] args) {

        URLFail test0 = null;
        URLFail test1 = null;
        URLFail test2 = null;

        try {
            test0 = new URLFail();
            System.out.println("First Complete...");
            Thread.sleep(1000);
            test2 = new URLFail();
            System.out.println("Second Complete...");
            Thread.sleep(1000);
            test1 = new URLFail();
            System.out.println("Third Complete...");
        } catch (InterruptedException e) {
            // TODO: handle exception
        }

        System.out.println();
        System.out.println("0Param " + test0.name);
        System.out.println("1Param " + test1.name);
        System.out.println("2Param " + test2.name);

    }

    public URLFail() {
        name = getAsList();
    }

    public int getAsList() {

        URL url = null;
        URLConnection urlConnection = null;
        try {
            url = new URL("xxxxx");
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
            return -1;
        }

        ArrayList<String> index = new ArrayList<String>();

        try {
            urlConnection = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream()));

            while (in.ready()) {
                index.add(in.readLine());
            }

            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            return -1;
        }

        if (index.isEmpty()) {
            return -1;
        } else {
            return 1;
        }

    }

}

我在 google.com 上尝试过,效果很好,所以它一定是我要连接的主机。有没有办法关闭 URL 连接/断开连接?

I'm having some trouble getting URL connection to work. I find that if I create one URL object it works fine. But everything after that doesn't work at all. At first I thought it could have been due to me not closing a stream but that was not the case.

-1 = Failure

1 = success

Testing:
        URLFail test0 = new URLFail();
        URLFail test1 = new URLFail(1);
        URLFail test2 = new URLFail(1,1);

OutPut:
    0Param  1
    1Param -1
    2Param -1

Now if i switch which object gets create first the results change.

Testing:
        URLFail test2 = new URLFail(1,1);
        URLFail test0 = new URLFail();
        URLFail test1 = new URLFail(1);

OutPut:
      0Param -1
      1Param -1
      2Param  1

In this instance only test2 succeeded. This leads me to believe that only one URL stream can be used per class?

This is what i've been using to access and parse the URL.

        URL url = null;
        URLConnection urlConnection = null;
        try {
            url = new URL("xxxxx"); // In my program this is a proper URL

        } catch (MalformedURLException ex) {
            return -1;
        }

        ArrayList<String> index = new ArrayList<String>();

        try {
            urlConnection = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            while (in.ready()) {
                index.add(in.readLine());
            }

            in.close();
        } catch (IOException ex) {
            return -1;

        }

So far I don't really know what's wrong. I think it could be due to the fact that I'm trying to access the same website too fast?

I've also tried without a URLConnection object by using the openStream() method found in URL. Any help will be appreciated, also any general comments about the code itself are welcomed as well. Thanks!

This is the Entire Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class URLFail {
    private int name;

    public static void main(String[] args) {

        URLFail test0 = null;
        URLFail test1 = null;
        URLFail test2 = null;

        try {
            test0 = new URLFail();
            System.out.println("First Complete...");
            Thread.sleep(1000);
            test2 = new URLFail();
            System.out.println("Second Complete...");
            Thread.sleep(1000);
            test1 = new URLFail();
            System.out.println("Third Complete...");
        } catch (InterruptedException e) {
            // TODO: handle exception
        }

        System.out.println();
        System.out.println("0Param " + test0.name);
        System.out.println("1Param " + test1.name);
        System.out.println("2Param " + test2.name);

    }

    public URLFail() {
        name = getAsList();
    }

    public int getAsList() {

        URL url = null;
        URLConnection urlConnection = null;
        try {
            url = new URL("xxxxx");
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
            return -1;
        }

        ArrayList<String> index = new ArrayList<String>();

        try {
            urlConnection = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream()));

            while (in.ready()) {
                index.add(in.readLine());
            }

            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            return -1;
        }

        if (index.isEmpty()) {
            return -1;
        } else {
            return 1;
        }

    }

}

I tried this with google.com and it worked fine, so it must be the host that I'm trying to connect to. Is there any way to close a URL connection/disconnect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

累赘 2024-09-19 04:29:56

在每个“return -1”前面插入一个

ex.printStackTrace();

这很可能会告诉您需要了解的内容。

In front of each "return -1" insert a

ex.printStackTrace();

This will most likely tell you what you need to know.

眼中杀气 2024-09-19 04:29:56

那么,当连续运行三次时,使用完整示例代码的“问题”服务器上的行为是什么?如果运行几次,它总是会产生相同的结果吗?

是否可能

in.ready()

在第一次在循环中检查时简单地返回 false,因为您的目标服务器根本没有足够快地发送响应?

So what's your behaviour on your "problem" server with the full example code when running it three times in a row? Does it always produce the same result if you run it a couple of times?

Could it be that

in.ready()

simply returns false the first time it's checked in the loop, because your target server simply doesn't send the response quickly enough?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文