• 读取文档并打印


    http.txt文档内容如下

    POST /login HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
    Referer: http://127.0.0.1:8088/1.html
    Accept-Language: zh-cn
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
    Host: 127.0.0.1:8088
    Content-Length: 19
    Connection: Keep-Alive
    Cache-Control: no-cache

    userName=qq&pwd=111&age=20&edu=大专

    要求:将第一行post封装到method属性、/login封装到url、HTTP/1.1封装到protocol属性。第二行到倒数第三行封装到headMap<String, String>,最后一行封装到paramMap<String, String>.


    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class Request {
      private String method;
      private String protocol;
      private String url;
      private Map<String, String> headMap = new HashMap<String, String>();
      private Map<String, String> paramMap = new HashMap<String, String>();
      public Request(){
        Reader r = null;
        BufferedReader br = null;
        List<String> list = new ArrayList<String>();
        try {
          r = new FileReader("http.txt");
          br = new BufferedReader(r);
          String str = "";
          while((str = br.readLine())!= null){
            list.add(str);
          }

        } catch (Exception e) {
          e.printStackTrace();
        }finally {
          try {
            br.close();
            r.close();
          } catch (IOException e) {
            e.printStackTrace();
          }

        }
        this.pressLine(list.get(0));
        this.pressHead(list);
        this.pressPara(list.get(list.size()-1));
      }

      private void pressLine(String line) {
        String[] array = line.split(" ");
        this.method = array[0];
        this.url = array[1];
        this.protocol = array[2];
      }
      private void pressHead(List<String> list) {
        for(int i = 1; i < list.size()-2; i++){
        String[] array1 = list.get(i).split(": ");
        this.headMap.put(array1[0], array1[1]);
        }
     }
      private void pressPara(String s) {
        String[] array2 = s.split("&");
        for(String a : array2){
          String[] str1 = a.split("=");
          this.paramMap.put(str1[0], str1[1]);
        }
      }

      public String getMethod() {
        return this.method;
      }
      public String getProtocol() {
        return this.protocol;
      }

      public String getUrl() {
        return this.url;
      }

      public String getHeadMap(String key) {
        return this.headMap.get(key);
      }

      public String getParamMap(String key) {
        return this.paramMap.get(key);
      }

      @Override
      public String toString() {
        return "Request [method=" + method + ", protocol=" + protocol + ", url=" + url + ", headMap=" + headMap
          + ", paramMap=" + paramMap + "]";
      }

     }

    class RequestTest {

      public static void main(String[] args) {
        Request r = new Request();
        String method = r.getMethod();
        String url = r.getUrl();
        String protocol = r.getProtocol();
        String headValue = r.getHeadMap("Host");
        String paraValue = r.getParamMap("userName");
        System.out.println(method+" "+url+" "+protocol+" "+paraValue+" "+headValue);
      }

    }

  • 相关阅读:
    go语言Notepad++简易开发环境搭建(windows)
    openssl AES加密以及padding
    为什么数据库要读写分离
    关于查询服务器文件是否过期的分析
    linux 禁止指定账号ssh登陆
    libmemcached upcoming ISO C++ standard, C++0x
    keepalived安装配置(nginx)
    php连接mysql报错No such file or directory
    linux命令行下使用R语言绘图
    纯真IP数据库导入mysql
  • 原文地址:https://www.cnblogs.com/tcc89/p/5402099.html
Copyright © 2020-2023  润新知