6.3. URL与URLConnection

2023/6/2 23:22:11

本文主要是介绍6.3. URL与URLConnection,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. 什么是URL?

URL(Uniform Resource Locator,统一资源定位符)是一个指向互联网上某个资源的地址。URL通常包括以下几个部分:协议、主机名、端口号(可选)和资源路径。例如,https://www.example.com:80/index.html是一个URL,其中https是协议,www.example.com是主机名,80是端口号,/index.html是资源路径。

2. Java中的URL类

在Java中,java.net.URL类可以用于表示一个URL。URL类提供了一些方法,以便我们可以轻松地访问和操作URL的各个部分。以下是一些常用方法:

  • URL(String spec):根据指定的字符串创建一个URL对象。
  • URL(String protocol, String host, int port, String file):根据指定的协议、主机名、端口号和文件名创建一个URL对象。
  • String getProtocol():获取URL的协议部分。
  • String getHost():获取URL的主机名部分。
  • int getPort():获取URL的端口号部分。
  • String getFile():获取URL的文件(资源路径)部分。

3. 使用URL读取网络资源

使用URL类,我们可以轻松地访问和读取互联网上的资源。以下是一个简单示例,用于读取网页的内容:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class URLExample {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://www.example.com/");
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4. 什么是URLConnection?

java.net.URLConnection类表示应用程序和URL之间的通信链接。它提供了一组方法,用于读取和写入网络资源的数据。URLConnection类的常用方法有:

  • void connect():建立到URL引用的资源的通信链接(如果尚未建立这样的连接)。
  • InputStream getInputStream():获取一个输入流,用于从URLConnection读取数据。
  • OutputStream getOutputStream():获取一个输出流,用于向URLConnection写入数据。
  • void setDoOutput(boolean dooutput):设置是否允许输出数据。 默认为false。
  • void setDoInput(boolean doinput):设置是否允许输入数据。 默认为true。

5. 使用URLConnection读取和写入网络资源

以下是一个简单的示例,演示如何使用URLConnection从网络资源读取数据:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionExample {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://www.example.com/");
            URLConnection connection = url.openConnection();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

下面的示例演示了如何使用HttpURLConnection(URLConnection的子类)向服务器发送POST请求并获取响应:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpURLConnectionPOSTExample {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://www.example.com/login");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);

            String postData = "username=user&password=pass";
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(postData.getBytes("UTF-8"));
            outputStream.flush();
            outputStream.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这个示例中,我们首先创建了一个HttpURLConnection对象,并设置请求方法为POST。然后,我们通过调用setDoOutput(true)setDoInput(true)允许输入输出。接下来,我们将POST数据写入输出流,然后从输入流中读取服务器响应。

这就是关于Java网络编程中的URL和URLConnection的介绍。希望这些示例和解释能帮助你更好地理解这个概念。祝你学习愉快!



这篇关于6.3. URL与URLConnection的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程