首页

javascript学习指南|JAVA如何获取HTTP请求头

热门范文 |

时间:

2016-06-07

|

推荐访问

java学习路线

【www.qhjz66.com--热门范文】

  在利用Java网络编程时,常常需要获取HTTP请求头,那么JAVA如何获取HTTP请求头呢?下面小编为大家介绍了JAVA获取HTTP请求头的方法,希望能帮到大家!

  在利用Java网络编程时,利用Java获取HTTP Request 和 Response头字段,可以利用Java语言根据需要添加自定义的HTTP头字段,而不必拘泥于标准HTTP定义的头字段。

  代码如下:

  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();

  }

  }

  运行结果:

  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

延伸阅读
【成语名字】春色撩人【汉语拼音】chūn sè liáo rén【近义词】:通都大邑、鱼米之乡
2021-06-09
夜越来越黑了,一片幽静包裹一着整个夜空。在那冷艳的夜空下,桃花的粉一红显得那么苍白,一阵微风吹过,似乎要吹落桃花的羞涩。山上的月夜,轻柔
2021-06-09
夜,又笼上一层神秘的色彩,让我百思不解捉摸不透。 我喜欢独自一人站在阳台上看夜景,默默地感受那份白天无法得到的景致。路灯就象一个个昏昏欲睡
2021-06-09
1 告诉你,一想到你,我这张丑脸上就泛起微笑。还有在我安静的时候,你就从我内心深处浮现,就好像阿芙罗蒂从浪花里浮现一样。  2 祝你今天
2021-06-09
下面是小编为大家收集的关于撩人心弦的句子,希望大家喜欢!岁月如流沙 青丝换白发 纵使回忆如画 思念却是一刹谁的再见 引发的泪如雨下 曾经繁华
2021-06-09
1、 一杯名茶,予人生以幽香;一壶老酒,予人生以欢畅;一首小诗,予生命以凝望;一条短信,予朋友以愿望。幸福就是这么简单,愿友一生平安。
2021-06-09
在我的家乡富盛有一条清澈见底的河,河随山而转,而这座美丽的山,名曰:凤凰山。听我奶奶讲这里还有一个动人的传说呢: 相传在很久很久以前,有
2021-06-09
朋友要出门,该送什么祝福语呢?下面小编整理了朋友要出门祝福话语,欢迎大家阅读学习!  朋友要出门祝福话语  1、伴随着任何科学人才的出现
2021-06-09
做生意或刚开业的营业店,都需要生意兴隆祝福语,下面小编整理的做生意的祝福语,欢迎来参考!  1、在开业的今天,祝贺的话语都斟满酒杯,溢出
2021-06-09
在我的家乡富盛有一条清澈见底的河,河随山而转,而这座美丽的山,名曰:凤凰山。听我奶奶讲这里还有一个动人的传说呢: 相传在很久很久以前,有一
2021-06-09
  • 推荐访问