当前位置: 首页>>代码示例>>Java>>正文


Java MessageHeader.parseHeader方法代码示例

本文整理汇总了Java中sun.net.www.MessageHeader.parseHeader方法的典型用法代码示例。如果您正苦于以下问题:Java MessageHeader.parseHeader方法的具体用法?Java MessageHeader.parseHeader怎么用?Java MessageHeader.parseHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sun.net.www.MessageHeader的用法示例。


在下文中一共展示了MessageHeader.parseHeader方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createMessageHeader

import sun.net.www.MessageHeader; //导入方法依赖的package包/类
static MessageHeader createMessageHeader (String s) {
    ByteArrayInputStream bis = new ByteArrayInputStream (s.getBytes());
    MessageHeader h = new MessageHeader ();
    try {
        h.parseHeader (bis);
    } catch (IOException e) {
        throw new RuntimeException ("IOException parsing header");
    }
    return h;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:11,代码来源:HeaderTests.java

示例2: test

import sun.net.www.MessageHeader; //导入方法依赖的package包/类
/**
 * Test the http protocol handler with the given authentication schemes
 * in the WWW-Authenticate header.
 */
static void test(String... schemes) throws IOException {

    // the authentication scheme that the client is expected to choose
    String expected = null;
    for (String s: schemes) {
        if (expected == null) {
            expected = s;
        } else if (s.equals("Digest")) {
            expected = s;
        }
    }

    // server reply
    String reply = authReplyFor(schemes);

    System.out.println("====================================");
    System.out.println("Expect client to choose: " + expected);
    System.out.println(reply);

    try (ServerSocket ss = new ServerSocket(0)) {
        Client.start(ss.getLocalPort());

        // client ---- GET ---> server
        // client <--- 401 ---- server
        try (Socket s = ss.accept()) {
            new MessageHeader().parseHeader(s.getInputStream());
            s.getOutputStream().write(reply.getBytes("US-ASCII"));
        }

        // client ---- GET ---> server
        // client <--- 200 ---- server
        String auth;
        try (Socket s = ss.accept()) {
            MessageHeader mh = new MessageHeader();
            mh.parseHeader(s.getInputStream());
            s.getOutputStream().write(OKAY.getBytes("US-ASCII"));
            auth = mh.findValue("Authorization");
        }

        // check Authorization header
        if (auth == null)
            throw new RuntimeException("Authorization header not found");
        System.out.println("Server received Authorization header: " + auth);
        String[] values = auth.split(" ");
        if (!values[0].equals(expected))
            throw new RuntimeException("Unexpected value");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:53,代码来源:NoNTLM.java

示例3: parse

import sun.net.www.MessageHeader; //导入方法依赖的package包/类
private Map<String, List<String>> parse(InputStreamWrapper input)
     throws IOException
{
    // The bulk of work is done by this time-proven class
    MessageHeader h = new MessageHeader();
    h.parseHeader(input);

    // When there are no headers (and therefore no body), the status line
    // will be followed by an empty CRLF line.
    // In that case MessageHeader.parseHeader() will consume the first
    // CR character and stop there. In this case we must consume the
    // remaining LF.
    if (input.consumed == 1 && CR == (char) input.lastRead) {
        // MessageHeader will not consume LF if the first character it
        // finds is CR. This only happens if there are no headers, and
        // only one byte will be consumed from the buffer. In this case
        // the next byte MUST be LF
        if (input.read() != LF) {
            throw new IOException("Unexpected byte sequence when no headers: "
                 + ((int)CR) + " " + input.lastRead
                 + "(" + ((int)CR) + " " + ((int)LF) + " expected)");
        }
    }

    Map<String, List<String>> rawHeaders = h.getHeaders();

    // Now some additional post-processing to adapt the results received
    // from MessageHeader to what is needed here
    Map<String, List<String>> cookedHeaders = new HashMap<>();
    for (Map.Entry<String, List<String>> e : rawHeaders.entrySet()) {
        String key = e.getKey();
        if (key == null) {
            throw new ProtocolException("Bad header-field");
        }
        if (!isValidName(key)) {
            throw new ProtocolException(format(
                    "Bad header-name: '%s'", key));
        }
        List<String> newValues = e.getValue();
        for (String v : newValues) {
            if (!isValidValue(v)) {
                throw new ProtocolException(format(
                        "Bad header-value for header-name: '%s'", key));
            }
        }
        String k = key.toLowerCase(Locale.US);
        cookedHeaders.merge(k, newValues,
                (v1, v2) -> {
                    ArrayList<String> newV = new ArrayList<>();
                    if (v1 != null) {
                        newV.addAll(v1);
                    }
                    newV.addAll(v2);
                    return newV;
                });
    }
    return cookedHeaders;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:59,代码来源:ResponseHeaders.java


注:本文中的sun.net.www.MessageHeader.parseHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。