當前位置: 首頁>>代碼示例>>Java>>正文


Java Headers.put方法代碼示例

本文整理匯總了Java中com.sun.net.httpserver.Headers.put方法的典型用法代碼示例。如果您正苦於以下問題:Java Headers.put方法的具體用法?Java Headers.put怎麽用?Java Headers.put使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.sun.net.httpserver.Headers的用法示例。


在下文中一共展示了Headers.put方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: handle

import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void handle(HttpExchange httpExchange) throws IOException {
    StringBuilder body = new StringBuilder();
    try (InputStreamReader reader = new InputStreamReader(httpExchange.getRequestBody(), Consts.UTF_8)) {
        char[] buffer = new char[256];
        int read;
        while ((read = reader.read(buffer)) != -1) {
            body.append(buffer, 0, read);
        }
    }
    Headers requestHeaders = httpExchange.getRequestHeaders();
    Headers responseHeaders = httpExchange.getResponseHeaders();
    for (Map.Entry<String, List<String>> header : requestHeaders.entrySet()) {
        responseHeaders.put(header.getKey(), header.getValue());
    }
    httpExchange.getRequestBody().close();
    httpExchange.sendResponseHeaders(statusCode, body.length() == 0 ? -1 : body.length());
    if (body.length() > 0) {
        try (OutputStream out = httpExchange.getResponseBody()) {
            out.write(body.toString().getBytes(Consts.UTF_8));
        }
    }
    httpExchange.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:25,代碼來源:RestClientSingleHostIntegTests.java

示例2: handle

import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void handle(HttpExchange t) throws IOException {
    Headers reqHeaders = t.getRequestHeaders();

    // some small sanity check
    List<String> cookies = reqHeaders.get("Cookie");
    for (String cookie : cookies) {
        if (!cookie.contains("JSESSIONID")
            || !cookie.contains("WILE_E_COYOTE"))
            t.sendResponseHeaders(400, -1);
    }

    // return some cookies so we can check getHeaderField(s)
    Headers respHeaders = t.getResponseHeaders();
    List<String> values = new ArrayList<>();
    values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH);
    values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path="
               + URI_PATH +"; HttpOnly");
    values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH);
    respHeaders.put("Set-Cookie", values);
    values = new ArrayList<>();
    values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path="
               + URI_PATH);
    respHeaders.put("Set-Cookie2", values);
    values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100)
               + "; version=1; Path=" + URI_PATH +"; HttpOnly");
    respHeaders.put("Set-Cookie2", values);

    t.sendResponseHeaders(200, -1);
    t.close();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:32,代碼來源:HttpOnly.java

示例3: setResponseHeaders

import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void setResponseHeaders(Map<String,List<String>> headers) {
    Headers r = httpExchange.getResponseHeaders();
    r.clear();
    for(Map.Entry <String, List<String>> entry : headers.entrySet()) {
        String name = entry.getKey();
        List<String> values = entry.getValue();
        // ignore headers that interfere with our correct operations
        if (!"Content-Length".equalsIgnoreCase(name) && !"Content-Type".equalsIgnoreCase(name)) {
            r.put(name,new ArrayList<String>(values));
        }
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:14,代碼來源:ServerConnectionImpl.java

示例4: setResponseHeaders

import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
@Override
public void setResponseHeaders(Map<String,List<String>> headers) {
    Headers r = httpExchange.getResponseHeaders();
    r.clear();
    for(Map.Entry <String, List<String>> entry : headers.entrySet()) {
        String name = entry.getKey();
        List<String> values = entry.getValue();
        // ignore headers that interfere with our correct operations
        if (!name.equalsIgnoreCase("Content-Length") && !name.equalsIgnoreCase("Content-Type")) {
            r.put(name,new ArrayList<String>(values));
        }
    }
}
 
開發者ID:alexkasko,項目名稱:openjdk-icedtea7,代碼行數:14,代碼來源:ServerConnectionImpl.java

示例5: setDefaultContentType

import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
private void setDefaultContentType(Headers httpHeaders) {
    if (defaultContentType != null) {
        httpHeaders.put(CONTENT_TYPE, Collections.singletonList(defaultContentType));
    }
}
 
開發者ID:rockem,項目名稱:blink-java,代碼行數:6,代碼來源:HttpExchangeBlinkResponse.java


注:本文中的com.sun.net.httpserver.Headers.put方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。