我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

JAVA获取HTTP请求头的方法示例-创新互联

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

10年积累的成都做网站、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有鄂托克免费网站建设让你可以放心的选择与我们合作。

在利用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();
  }
}


网站名称:JAVA获取HTTP请求头的方法示例-创新互联
链接地址:http://mswzjz.cn/article/esocc.html

其他资讯