在Java中的Java.net.HttpURLConnection类和示例中,我们了解了如何创建URL对象并获取Internet上任何资源的信息。但是,获取状态信息几乎不是真实世界应用程序的真正动机。要检索信息,处理信息并将结果发送回服务器,或者只是显示从服务器检索到的所需信息,这是我们的目标。例如,考虑一个小应用程序,它向用户请求电影名称,然后返回电影的“imdb”评级或返回与该电影相关的所有链接。所有这些都可以使用URLConnection类来实现。
什么是URLConnection类
URLConnection是一个抽象类,其子类构成用户应用程序与Web上任何资源之间的链接。我们可以使用它来读取/写入URL对象引用的任何资源。
主要有两个子类扩展了URLConnection类 -
HttpURLConnection:如果我们连接到任何使用“http”作为协议的url,则使用HttpURLConnection类。
JarURLConnection:但是,如果我们尝试在Web上建立与jar文件的连接,则使用JarURLConnection。
一旦建立连接并且我们有一个URLConnection对象,我们就可以使用它来读取或写入或获取有关页面/文件上次修改的时间,内容长度等的进一步信息。
URLConnection类的重要方法
URLConnection openConnection():打开与指定URL的连接。
Object getContent():检索URLConnection的内容。
Map getHeaderFields():返回包含http标头中各种标题字段值的映射。
getContentEncoding():返回content-encoding头字段的值。
getContentLength():返回内容标题字段的长度。
getDate():在header字段中返回date的值。
getHeaderField(int i):返回标头的第 i 个索引的值。
getHeaderField(String field):返回标题中名为“field”的字段的值
OutputStream getOutputStream():将输出流返回到此连接。
InputStream getInputStream():将输入流返回到此打开的连接。
setAllowUserInteraction(boolean):设置为true表示用户可以与页面进行交互。默认值为true。
setDefaultUseCaches(boolean):将useCache字段的默认值设置为给定值。
setDoInput(boolean):设置是否允许用户接受输入。
setDoOutput(boolean):设置是否允许用户在页面上写入。默认值为false,因为大多数url不允许写入。
让我们看一个示例程序,它使用上面的方法显示标题字段,并将整个页面的源代码打印到控制台窗口。
//Java Program to illustrate reading and writing
// in URLConnection Class
import java.io.*;
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 URLConnectionclass
{
public static void main(String[] args)
{
try
{
URL url = new URL("https://www.breakyizhan.com");
//open the connection to the above URL.
URLConnection urlcon = url.openConnection();
//Executing the below line would print the value of
// Allow User Interaction field.
// System.out.println(urlcon.getAllowUserInteraction());
//Executing the below line would print
// the value of Content Type field.
// System.out.println(urlcon.getContentType());
//Executing the below line would print the value
// of URL of the given connection.
// System.out.println(urlcon.getURL());
//Executing the below line would
// print the value of Do Input field.
// System.out.println(urlcon.getDoInput());
//Executing the below line would
// print the value of Do Output field.
// System.out.println(urlcon.getDoOutput());
//Executing the below line would
// print the value of Last Modified field.
// System.out.println(new Date(urlcon.getLastModified()));
//Executing the below line would
// print the value of Content Encoding field.
// System.out.println(urlcon.getContentEncoding());
//To get a map of all the fields of http header
Map> header = urlcon.getHeaderFields();
//print all the fields along with their value.
for (Map.Entry> mp : header.entrySet())
{
System.out.print(mp.getKey() + " : ");
System.out.println(mp.getValue().toString());
}
System.out.println();
System.out.println("Complete source code of the URL is-");
System.out.println("---------------------------------");
//get the inputstream of the open connection.
BufferedReader br = new BufferedReader(new InputStreamReader
(urlcon.getInputStream()));
String i;
//print the source code line by line.
while ((i = br.readLine()) != null)
{
System.out.println(i);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
输出:
Keep-Alive : [timeout=5, max=100]
null : [HTTP/1.1 200 OK]
Server : [Apache/2.4.18 (Ubuntu)]
Connection : [Keep-Alive]
Last-Modified : [Wed, 16 Nov 2016 06:49:55 GMT]
Date : [Wed, 16 Nov 2016 10:58:34 GMT]
Accept-Ranges : [bytes]
Cache-Control : [max-age=3]
ETag : ["10866-541657b07e4d7"]
Vary : [Accept-Encoding]
Expires : [Wed, 16 Nov 2016 10:58:37 GMT]
Content-Length : [67686]
Content-Type :
Complete source code of the URL is-
--------------------------------------------------
...source code of the page...
6253




被折叠的 条评论
为什么被折叠?



