RawHttp

作者: 刘书生 | 来源:发表于2019-10-30 14:45 被阅读0次

    1.参考资料

    gitup地址:https://github.com/renatoathaydes/rawhttp
    博客地址:https://sites.google.com/a/athaydes.com/renato-athaydes/posts/announcingrawhttp-ajvmlibraryforhandlingrawhttp

    2.介绍

    用于处理原始HTTP的JVM库,

    3.请求/响应格式

    1.请求格式
    GET / HTTP/1.1
    Host: headers.jsontest.com
    Accept: application/json
    
    2.响应格式
    HTTP/1.1 200 OK
    Access-Control-Allow-Origin: *
    Content-Type: application/json; charset=ISO-8859-1
    X-Cloud-Trace-Context: e7be085086214dffdeb6350ad37672ed
    Date: Sun, 10 Dec 2017 18:33:53 GMT
    Server: Google Frontend
    Content-Length: 155
    
    {
       "X-Cloud-Trace-Context": "e7be085086214dffdeb6350ad37672ed/9779089525915638501",
       "Host": "headers.jsontest.com",
       "Accept": "application/json"
    }
    

    4.在java中的使用

    1.第一步:引入依赖
    <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>3.10.0</version>
            </dependency>
    
    2.第二步:编写demo
    @Test
    public void contextLoads() throws IOException {
            RawHttp rawHttp = new RawHttp();
    
            // Host里面包含主机和端口
            RawHttpRequest request = rawHttp.parseRequest(
                    "GET /hello.txt HTTP/1.1\r\n" +
                            "User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3\r\n" +
                            "Host: http://localhost:9596\r\n" +
                            "Accept-Language: en, mi\r\n" +
                            "x-forwarded-for: en, mi");
            System.out.println(request.getUri().getPort());
     }
    

    输出

    image.png

    相关文章

      网友评论

          本文标题:RawHttp

          本文链接:https://www.haomeiwen.com/subject/cuvovctx.html