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的介绍。希望这些示例和解释能帮助你更好地理解这个概念。祝你学习愉快!
推荐阅读:

https://mp.weixin.qq.com/s/dV2JzXfgjDdCmWRmE0glDA

https://mp.weixin.qq.com/s/an83QZOWXHqll3SGPYTL5g

作者:移动安全星球原文地址:https://segmentfault.com/a/1190000043863110

%s 个评论

要回复文章请先登录注册