本文整理汇总了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));
}
}