首頁 > 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學習者快速成長!