服务器之家

服务器之家 > 正文

JAVA获取HTTP请求头的方法示例

时间:2020-11-23 13:16     来源/作者:tterminator

本文实例讲述了JAVA获取HTTP请求头的方法。分享给大家供大家参考,具体如下:

在利用Java网络编程时,利用Java获取HTTP Request 和 Response头字段;

可以利用Java语言根据需要添加自定义的HTTP头字段,而不必拘泥于标准HTTP定义的头字段。

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class TestURL {
  public static void main(String[] args) {
    String destURLStr= "http://www.baidu.com";
    URL destURL = null;
    URLConnection urlCon = null;
    HttpURLConnection httpUrlCon= null;
    String readResFile = "C:/Users/zhoujw/Desktop/readResFile.html";
    BufferedWriter bw = null;
    try {
      bw = new BufferedWriter(new FileWriter(readResFile));
      destURL = new URL(destURLStr);
      urlCon = destURL.openConnection();
      httpUrlCon = (HttpURLConnection)urlCon;
      //set request property
      httpUrlCon.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
      //可根据需要添加自定义请求头
      httpUrlCon.setRequestProperty("Test Header1", "test1");
      httpUrlCon.setRequestProperty("Test Header2", "test2");
      httpUrlCon.connect();
      BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlCon.getInputStream(), "gbk"));
      String webpage = null;
      while((( webpage = br.readLine()) != null))
      {
//       System.out.println(webpage);
        bw.write(webpage);
        bw.flush();
      }
      //debug
      System.out.println("Self Define Headers:");
      System.out.println(" Test Header1: " + httpUrlCon.getRequestProperty("Test Header1"));
      System.out.println(" Test Header2: " + httpUrlCon.getRequestProperty("Test Header2"));
      System.out.println();
      //echo request property
      echoRequestHeaders(httpUrlCon);
      //echo response property
      echoResponseHeaders(httpUrlCon);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public static void echoRequestHeaders(HttpURLConnection httpUrlCon){
    System.out.println("Request Headers:");
    System.out.println(" " + httpUrlCon.getRequestMethod() + " / " + " HTTP/1.1");
    System.out.println(" Host: " + httpUrlCon.getRequestProperty("Host"));
    System.out.println(" Connection: " + httpUrlCon.getRequestProperty("Connection"));
    System.out.println(" Accept: " + httpUrlCon.getRequestProperty("Accept"));
    System.out.println(" User-Agent: " + httpUrlCon.getRequestProperty("User-Agent"));
    System.out.println(" Accept-Encoding: " + httpUrlCon.getRequestProperty("Accept-Encoding"));
    System.out.println(" Accept-Language: " + httpUrlCon.getRequestProperty("Accept-Language"));
    System.out.println(" Cookie: " + httpUrlCon.getRequestProperty("Cookie"));
    System.out.println(" Connection: " + httpUrlCon.getHeaderField("Connection"));//利用另一种读取HTTP头字段
    System.out.println();
  }
  public static void echoResponseHeaders(HttpURLConnection httpUrlCon) throws IOException{
    System.out.println("Response Headers:");
    System.out.println(" " + "HTTP/1.1 " + httpUrlCon.getResponseCode() + " " + httpUrlCon.getResponseMessage());
    System.out.println(" status: " + httpUrlCon.getResponseCode() + " " + httpUrlCon.getResponseMessage());
    System.out.println(" content-encoding: " + httpUrlCon.getContentEncoding());
    System.out.println(" content-length : " + httpUrlCon.getContentLength());
    System.out.println(" content-type: " + httpUrlCon.getContentType());
    System.out.println(" Date: " + httpUrlCon.getDate());
    System.out.println(" ConnectTimeout: " + httpUrlCon.getConnectTimeout());
    System.out.println(" expires: " + httpUrlCon.getExpiration());
    System.out.println(" content-type: " + httpUrlCon.getHeaderField("content-type"));//利用另一种读取HTTP头字段
    System.out.println();
  }
}

运行结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Self Define Headers:
 Test Header1: test1
 Test Header2: test2
Request Headers:
 GET / HTTP/1.1
 Host: www.baidu.com
 Connection: keep-alive
 Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
 User-Agent: Java/1.6.0_20
 Accept-Encoding: gzip,deflate,sdch
 Accept-Language: null
 Cookie: null
 Connection: Keep-Alive
http://www.baidu.com
Response Headers:
 HTTP/1.1 200 OK
 status: 200 OK
 content-encoding: gzip
 content-length : -1
 content-type: text/html; charset=utf-8
 Date: 1427817028000
 ConnectTimeout: 0
 expires: 1427817001000
 content-type: text/html; charset=utf-8

希望本文所述对大家java程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部