楼主: Lisrelchen
1656 0

Using a URLConnection using Java [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-6-5 11:00:02 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Using a URLConnection using Java


The URLConnection class establishes a connection to a URL. The openStream( ) method of URL we used in Example 5-1 is merely a convenience method that creates a URLConnection object and calls its getInputStream( ) method. By using a URLConnection object directly instead of relying on openStream( ), you have much more control over the process of downloading the contents of a URL.

Example 5-2 is a simple program that shows how to use a URLConnection to obtain the content type, size, last-modified date, and other information about the resource referred to by a URL. If the URL uses the HTTP protocol, it also demonstrates how to use theHttpURLConnection subclass to obtain additional information about the connection.

Note the use of the java.util.Date class to convert a timestamp (a long that contains the number of milliseconds since midnight, January 1, 1970 GMT) to a human-readable date and time string.

  1. Example 5-2. GetURLInfo.java
  2. package je3.net;
  3. import java.net.*;
  4. import java.io.*;
  5. import java.util.Date;

  6. /**
  7. * A class that displays information about a URL.
  8. **/
  9. public class GetURLInfo {
  10.     /** Use the URLConnection class to get info about the URL */
  11.     public static void printinfo(URL url) throws IOException {
  12.         URLConnection c = url.openConnection( );  // Get URLConnection from URL
  13.         c.connect( );                             // Open a connection to URL
  14.         
  15.         // Display some information about the URL contents
  16.         System.out.println("  Content Type: " + c.getContentType( ));
  17.         System.out.println("  Content Encoding: " + c.getContentEncoding( ));
  18.         System.out.println("  Content Length: " + c.getContentLength( ));
  19.         System.out.println("  Date: " + new Date(c.getDate( )));
  20.         System.out.println("  Last Modified: " +new Date(c.getLastModified( )));
  21.         System.out.println("  Expiration: " + new Date(c.getExpiration( )));
  22.         
  23.         // If it is an HTTP connection, display some additional information.
  24.         if (c instanceof HttpURLConnection) {
  25.             HttpURLConnection h = (HttpURLConnection) c;
  26.             System.out.println("  Request Method: " + h.getRequestMethod( ));
  27.             System.out.println("  Response Message: " +h.getResponseMessage( ));
  28.             System.out.println("  Response Code: " + h.getResponseCode( ));
  29.         }
  30.     }
  31.    
  32.     /** Create a URL, call printinfo( ) to display information about it. */
  33.     public static void main(String[  ] args) {
  34.         try { printinfo(new URL(args[0])); }
  35.         catch (Exception e) {
  36.             System.err.println(e);
  37.             System.err.println("Usage: java GetURLInfo <url>");
  38.         }
  39.     }
  40. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:connection Connect Using Java ING contents directly control creates instead

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-2 04:00