JAVA [ HTTP ]

本文深入探讨了URL的基本组成、相对URL、URL编码规则以及HTTP请求消息的构成,包括请求行、消息头和实体内容。同时展示了如何通过代码实现获取网页内容,并通过设置Accept-Language头来获取不同语言版本的网页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 *  URL的基本组成:协议、主机名、端口号、资源名
 *  相对URL
 *  URL编码规则
 *   空格变为+
 *   对0-9a-zA-Z保持不变
 *   字符的当前字符集编码在内存中的十六进制格式表示,空格也可以%20,=%3D,&%26
 *  URLEncoder 和 URLDecoder
 * 
 * 建立连接
 * ----------------------------------->
 *  客户机              发出请求信息              服务器
 * ----------------------------------->
 * <----------------------------------
 * 发出相应信息
 * <----------------------------------
 *   关闭连接
 * 网页中图像只包含url,没有数据,多个图像需要一次有一次建立和关闭
 * http1.1,多个请求和应答可以再一次连接中完成
 * 
 * http请求消息
 * 一个请求行,若干个消息头,实体内容 [消息头和实体内容空行隔开]
 * GET /articles/news/today.asp HTTP/1.1 [请求行]
 * Accept:    [消息头]
 * Accpet-Language:en-us  [消息头]                 =======服务器返回的文档所使用的国家语言,多个以逗号隔开
 * Connection: Keep-Alive    [消息头]              =======处理完本地请求响应后,客户端与服务器是否继续保持连接【Keep-Alive、Close,1.1默认持久连接】
 * Host: localhost    [消息头]
 * Referer: http://localhost/links.axp    [消息头]
 * User-Agent: Mozilla/4.0(compatible;MSIE 5.5; Windows NT 5.0)   [消息头]
 * Accpet-Encoding:gzip,defalte   [消息头]
 *      Date: 
 * 1.1  Content-Type : text/html  [POST]
 *      Content-Length : 2291  [POST]                   ========实体内容的长度
 *      Range:byte=100-599                             ========只需返回文档中的部分内容
 *       byte=100-
 *       byte=-100
 *      Content-Range:byte 2543-4532/7898              =========服务器返回的部分实体内容的位置信息
 *      Set-Cookie: ASPSESSIONIDQQGGGNCG=safasdfaslfohefohla;hfsl;path=/
 *      Cache-control: private
 *
 *      <HTML>
 *       ....
 *      
 *get不能获得实体内容,post、delete、put可以
 *实验
 * telnet host port
 * GET /image/more3.gif HTTP/1.1
 * Host:
 *
 * telnet www.google.com 80
 * GET / HTTP/1.1
 * Host:
 * Accept-Language: zh-cn,ar-bh
 * Connection: close
 *
 *
 * @author ShenJie
 * 
 *  public URL(String spec)
 *  public URL(String protocol, String host, int port, String file)
 *  public URL(String protocol, String host, int port, String file, URLStreamHandler handler)  //处理器
 *  public URL(URL context, String spec)  目录资源、目录下的文件名
 *  
 *  getProtocol\getHost\getPort\getFile  不能获取具体内容
 *  openConnection方法调用URLStreamHandler协议处理器返回一个URLConnection对象,代表访问资源的链接
 *  setURLStreamHandlerFactory(URLStreamHandlerFacotry fac)静态方法
 *  createURLStreamHandler(String protocol)方法 
 *  
 *  URLConnection与HttpURLConnection类
 *  URLConncetion产生后,连接过程
 *  setRequestProperty方法,设置http协议的各个请求消息头
 *  getHeaderFields方法服务器会送的响应消息头
 *  getInputStream 和 getOutputStream方法,或得响应实体的内容
 *  getHeaderField\getContentLength\getContentEncoding\getContentType
 *  
 *  一个HTTP连接可以被多个HttpURLConnection实例对象共享,调用HttpURLConnection的disconnect关闭底层共享网络
 *  
 *  访问www.google.com站点的HTTP请求消息的Accept-Language头分别设置成日文和中文后,
 *  然后打印出www.google.com站点返回的所有响应消息头和网页内容
 *  
 */

public class URLDemo1 {
public static void main(String[] args){
System.out.println("获取日文页面");
getContentByLanguage("ja");
System.out.println();
System.out.println("获取中文页面");
getContentByLanguage("zh_cn");
}
public static void getContentByLanguage(String country){
try {
URL urlGoogle = new URL("http://www.google.com");
try {
HttpURLConnection googleConnection = 
(HttpURLConnection)urlGoogle.openConnection();
googleConnection.setRequestProperty("Accept-Language", country);

Map requests = googleConnection.getRequestProperties();
Set reqFields = requests.keySet();
Iterator itrReq = reqFields.iterator();
while(itrReq.hasNext()){
String field = (String)itrReq.next();
System.out.println( field + ": " + googleConnection.getRequestProperty(field));
}


//不调用connect方法效果一样
// googleConnection.connect();


Map responses = googleConnection.getHeaderFields();
Set resFields = responses.keySet();
Iterator itrRes = resFields.iterator();
while(itrRes.hasNext()){
String field = (String)itrRes.next();
System.out.println( field + ": " + googleConnection.getHeaderField(field));
}

InputStream is = googleConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String strLine = null;
while((strLine = br.readLine()) != null){
System.out.println( strLine);
}
br.close();
googleConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值