ホームページ  >  記事  >  Java  >  Java URL接続

Java URL接続

PHPz
PHPzオリジナル
2024-08-30 15:39:08618ブラウズ

URLConnection は、通常、通信リンクの 1 つ、または URL とアプリケーション間のリンクを表す Java プログラミング言語クラスです。この URLConnection クラスは、実際には URL によって参照される特定のリソースへのデータの読み取りと書き込みに役立ちます。これは、すべてのクラスのスーパークラスの 1 つです。この URLConnection クラスのインスタンスは、特定の URL によって参照されるリソースに対する読み取りと書き込みに役立ちます。ここで、特定の URL への接続の接続は、多段階プロセスの 1 つのタイプです。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文

URLConnection openConnection()

説明: URL クラスの open connection() メソッド/関数は、URLConnection クラスのオブジェクトを返します。

Jav で URLConnection はどのように機能しますかa

URLConnection クラスは、多くのメソッドを提供することで機能します。 URL を接続する複数の手順のプロセスには、openConnection() メソッドと connect() メソッドが含まれます。 openConnection() は、リモート リソース接続に影響を与える可能性のあるパラメータの操作に役立ちます。 connect() メソッドは、リソースとの対話に役立ち、クエリ ヘッダーの内容とフィールドに役立ちます。

接続オブジェクトは、URL 上で openConnection メソッドまたは関数を呼び出すだけで実際に作成されます。接続オブジェクトのセットアップ パラメータとプロパティの一般的なリクエストを操作します。リモート オブジェクトへの実際の通常の接続は、接続メソッドの使用を利用して行われます。そのリモート オブジェクトが使用可能になり、リモート オブジェクトの 1 つのヘッダー フィールドとその内容にアクセスできるようになります。 getInputStream() メソッドまたは関数は、特定のストリーム内の特定または指定された URL のすべてのデータを返すのに役立ち、読み取りと表示に使用できます。

Java プログラミング言語の URLConnection クラスは、実際には、InputStream() メソッドを取得するだけで Web ページまたはブログのすべてのデータを表示するために必要なだけのメソッドを提供することで機能しますが、getInputStream() メソッドは/function は、ストリーム内で言及されている特定の URL を使用してすべての Web サイト データを返すのに非常に役立ちます。この URL は、Web サイトまたはブログのソース コードの読み取りおよび表示に使用されます。すべてのソース コードを取得するには、複数の種類のソース コード表示にループを使用する必要があります。

Java の URLConnection クラスを拡張するサブクラスは 2 つだけです。それは、HttpURLConnection と JarURLConnection です。 HttpURLConnection は、実際にプロトコルとして「HTTP」を使用するあらゆるタイプの URL に接続するのに役立ちます。その場合、HttpURLConnection クラスが使用されます。 JarURLConnection は、World Wide Web 上の特定の jar ファイルへの接続の 1 つを確立するのに役立ちます。その場合、JarURLConnection が使用されます。

メソッド

重要なメソッドの一部は、接続の確立後に読み取りや書き込み、または情報の取得に使用すると役立ちます。それらは次のとおりです:

1. URLConnection openConnection(): このメソッドは、特定の URL または指定された URL への接続を開くのに役立ちます。

2. Object getContent(): URLConnection の一部のコンテンツを取得します。

3.マップ getHeaderFields(): 特定の HTTP フォルダー内のさまざまなヘッダー フィールドの値を含むマップを取得します。

4. getContentEncoding(): コンテンツ エンコーディング ヘッダーのフィールドの値を返します。

5. getContentLength(): コンテンツ ヘッダー フィールドの長さを返します。

6. getDate(): ヘッダーフィールドの日付値を返します

7. getHeaderField (int-i): ヘッダーの i 番目のインデックス値を返します

8. getHeaderField (String-Field): すべてのヘッダー フィールドのリストを取得するために、一部のヘッダーの値「field」という名前のフィールドを返します。

9. OutputStream getOutputStream(): 接続の出力ストリームの 1 つを返します。

10. InputStream getInputStream(): 開いている接続に 1 つの入力ストリームを返します。

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。