首页 > Java > java教程 > 正文

Java URL连接

PHPz
发布: 2024-08-30 15:39:08
原创
733 人浏览过

URLConnection 是一个 Java 编程语言类,通常表示 URL 与应用程序之间的通信链接或链接之一。这个 URLConnection 类帮助读取数据并将其写入特定/指定的资源,该资源实际上是由 URL 引用的。它是所有类的超类之一。 URLConnection 类的实例有助于读取和写入,并且针对特定 URL 引用的资源。这里将连接连接到特定 URL 是一种多步骤过程。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

URLConnection openConnection()
登录后复制

说明:URL 类的 open connection() 方法/函数将返回 URLConnection 类的对象。

URLConnection 在 Jav 中如何工作a

URLConnection 类通过提供许多方法来工作。连接 URL 的多个步骤过程中涉及到 openConnection() 和 connect() 方法。 openConnection() 有助于操作可能影响远程资源连接的参数。 connect() 方法有助于与资源进行交互,并且有助于查询标头内容和字段。

连接对象实际上是通过调用 URL 上的 openConnection 方法或函数来创建的。将操纵连接对象的设置参数和属性的一般请求。与远程对象的实际和通常的连接是在连接方法使用的帮助下建立的。它的远程对象变得可用,并且可以访问远程对象之一的标头字段及其内容。 getInputStream() 方法或函数将帮助返回特定流中特定或指定 URL 的所有数据,可用于读取和显示。

Java编程语言的URLConnection类实际上是通过提供尽可能多的方法来工作的,只是通过获取一个或多个InputStream()方法来显示网页或博客的所有数据,但是getInputStream()方法/function 在流中提到的特定 URL 的帮助下返回所有网站数据有很大帮助。该URL将用于读取和显示网站或博客的源代码;要获取所有源代码,必须使用 Loops 来实现多种类型的源代码显示。

只有两个子类扩展了Java的URLConnection类。它们是 HttpURLConnection 和 JarURLConnection。 HttpURLConnection 帮助我们连接到实际使用“HTTP”作为其协议的任何类型的 URL;然后,将使用 HttpURLConnection 类。 JarURLConnection 将帮助我们尝试建立与万维网上特定 jar 文件的连接;然后,将使用 JarURLConnection。

方法

一些重要的方法有助于在连接建立后用于读取或写入或获取一些信息。他们是:

1。 URLConnection openConnection(): 此方法有助于打开到特定或指定 URL 的连接。

2。 Object getContent(): 它将检索 URLConnection 的一些内容。

3。地图 getHeaderFields(): 它将获取包含特定 HTTP 文件夹中各种标头字段的一些值的映射。

4。 getContentEncoding(): 它将返回内容编码标头字段的一些值。

5。 getContentLength(): 它将返回内容标头字段的长度。

6。 getDate(): 它将返回标头字段的日期值

7。 getHeaderField (int-i): 它将返回 header 的第 i 个索引值

8。 getHeaderField (String-Field): 它将返回某个 header 中名为 value “field” 的字段,即获取所有 header 字段的列表。

9。 OutputStream getOutputStream(): 它将返回连接的输出流之一。

10。 InputStream getInputStream(): 它将返回一个输入流到打开的连接。

11。 setAllowUserInteraction(boolean): 它将把设置设置为 TRUE 值,这意味着用户可以与页面交互。默认情况下,它的值为 TRUE。

12。 setDefaultUseCaches(boolean): 它将把 useCache 字段的默认值设置为提供的值。

13。 setDoInput(boolean): 仅当用户是否允许接受特定输入时才会设置

14. setDoInput(boolean): It will set only if the user now allows writing on the specific page. By default, its value is FALSE since, most of all, the URL doesn’t even allow writing.

Examples to Implement Java URLConnection

below is the example of implementing java URLConnection:

Example #1

This illustrates the reading and writing of a blog/website URL using the URLConnection class. At first, different types of java libraries are imported. Then the public class is created along with the public main method for java code filling. Then the URL variable is created to add the specific website/blog URL with the help of the URL command. Then “URLConnection” is used to open a connection to the above-mentioned URL. Then Map is used to get all fields map of the specific HTTP header. Then to print all the fields of website URL and their values, FOR LOOP is used. Then BufferedReader is used to get the open connection’s inputstream. Then to print source code line by line, WHILE LOOP is used. While loop will print all the source code, the website/blog url mentioned in the code itself.

code:

import java.io.*;
//importing java input output functions
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class URLConnectionclass1
{
public static void main(String[] args)
{
try
{
URL url1 = new URL("https://www.profitloops.in");
URLConnection urlcon1 = url1.openConnection();
Map<String, List<String>> header = urlcon1.getHeaderFields();
for (Map.Entry<String, List<String>> mp1 : header.entrySet())
{
System.out.print(mp1.getKey() + " : ");
System.out.println(mp1.getValue().toString());
}
System.out.println();
System.out.println("The Complete source code of the provided URL is-");
System.out.println(":---------------------------------:");
BufferedReader br1 = new BufferedReader(new InputStreamReader
(urlcon1.getInputStream()));
String i1;
while ((i1 = br1.readLine()) != null)
{
System.out.println(i1);
}
}
catch (Exception e1)
{
System.out.println(e1);
}
}
}
登录后复制

Output:

Java URL连接

Conclusion

we hope you learned the definition of Java URLConnection and its syntax and explanation, How the URLConnection class works in Java Coding Language, and various examples to better understand the Java URLConnection concept and so easily.

以上是Java URL连接的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!