本文整理匯總了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();
}
示例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();
}
示例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));
}
}
}
示例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));
}
}
}
示例5: setDefaultContentType
import com.sun.net.httpserver.Headers; //導入方法依賴的package包/類
private void setDefaultContentType(Headers httpHeaders) {
if (defaultContentType != null) {
httpHeaders.put(CONTENT_TYPE, Collections.singletonList(defaultContentType));
}
}