欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-10 17:08:28
本文实例讲述了java获取http请求头的方法。分享给大家供大家参考,具体如下: 在利用java网络编程时,利用java获取http request 和 response...

本文实例讲述了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

更多关于java相关内容感兴趣的读者可查看本站专题:《java socket编程技巧总结》、《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

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