本文整理汇总了Java中com.sun.net.httpserver.Headers.set方法的典型用法代码示例。如果您正苦于以下问题:Java Headers.set方法的具体用法?Java Headers.set怎么用?Java Headers.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.Headers
的用法示例。
在下文中一共展示了Headers.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: noTriple
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private void noTriple(HttpExchange he) {
try {
Headers responseHeaders = he.getResponseHeaders();
FileHandler ext = new FileHandler();
File tmp;
responseHeaders.set(TYPE, PNG);
ext.getFile(NOTRIPLE);
tmp = new File(PAGESDIR + NOTRIPLE);
he.sendResponseHeaders(200, tmp.length());
try (OutputStream os = he.getResponseBody()) {
Files.copy(tmp.toPath(), os);
tmp.delete();
}
} catch (IOException ex) {
Logger.getLogger(CompareHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例2: setCors
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public static void setCors(Headers requestHeaders, Headers responseHeaders)
{
String origin = requestHeaders.getFirst("Origin");
if (origin == null)
origin = "null";
responseHeaders.set("Allow", "GET,PUT,POST,DELETE,OPTIONS");
responseHeaders.set("Access-Control-Allow-Origin", origin);
responseHeaders.set("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
responseHeaders.set("Access-Control-Allow-Headers", "Content-Type");
responseHeaders.set("Access-Control-Request-Methods", "GET,PUT,POST,DELETE,OPTIONS");
responseHeaders.set("Access-Control-Request-Headers", "Content-Type");
responseHeaders.set("Access-Control-Max-Age", "3600");
}
示例3: doHandle
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void doHandle(final HttpExchange t) throws IOException {
final String path = t.getRequestURI().getPath();
final int i = path.indexOf("/", 1);
final String cmd = path.substring(i + 1);
s_logger.info("Get CMD request for " + cmd);
if (cmd.equals("getstatus")) {
final ConsoleProxyClientStatsCollector statsCollector = ConsoleProxy.getStatsCollector();
final Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "text/plain");
t.sendResponseHeaders(200, 0);
final OutputStreamWriter os = new OutputStreamWriter(t.getResponseBody(), "UTF-8");
statsCollector.getStatsReport(os);
os.close();
}
}
示例4: handleClientStart
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private void handleClientStart(final HttpExchange t, final ConsoleProxyClient viewer, final String title, final String guest) throws IOException {
final List<String> languages = t.getRequestHeaders().get("Accept-Language");
final String response = viewer.onAjaxClientStart(title, languages, guest);
final Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "text/html");
hds.set("Cache-Control", "no-cache");
hds.set("Cache-Control", "no-store");
t.sendResponseHeaders(200, response.length());
final OutputStream os = t.getResponseBody();
try {
os.write(response.getBytes());
} finally {
os.close();
}
}
示例5: handle
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void handle(HttpExchange exchange) throws IOException {
String requestMethod = exchange.getRequestMethod();
if (requestMethod.equalsIgnoreCase("GET")) {
boolean isHealthy = serverWithStats.isHealthy();
Headers responseHeaders = exchange.getResponseHeaders();
String response = "{ \"version\" : \"" + serverWithStats.getVersion() + "\"}";
if (isHealthy) {
responseHeaders.set("Content-Type", "text/html");
exchange.sendResponseHeaders(200, response.getBytes().length);
} else {
responseHeaders.set("Content-Type", "text/html");
exchange.sendResponseHeaders(500, response.getBytes().length);
}
OutputStream responseBody = exchange.getResponseBody();
Headers requestHeaders = exchange.getRequestHeaders();
responseBody.write(response.getBytes());
responseBody.close();
}
}
示例6: handle
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void handle(HttpExchange exchange) throws IOException
{
/*InputStream is = t.getRequestBody();
String response = "response ok";
t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
*/
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/html");
String response = ConfigContext.getRequest(ConfigContext.getProp("RSPE404"));
exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
示例7: handle
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void handle(HttpExchange t) throws IOException {
StringWriter sw = new StringWriter();
SuPoxyDataObject data = SunnyList.get(SunnyList.size()-1);
sw.write(data.getJSONRaw());
String response = sw.toString();
Headers h = t.getResponseHeaders();
h.set("Content-Type","application/json");
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
示例8: doHandle
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
public void doHandle(HttpExchange t) throws Exception {
String path = t.getRequestURI().getPath();
int i = path.indexOf("/", 1);
String cmd = path.substring(i + 1);
s_logger.info("Get CMD request for " + cmd);
if (cmd.equals("getstatus")) {
ConsoleProxyClientStatsCollector statsCollector = ConsoleProxy.getStatsCollector();
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "text/plain");
t.sendResponseHeaders(200, 0);
OutputStreamWriter os = new OutputStreamWriter(t.getResponseBody(),"UTF-8");
statsCollector.getStatsReport(os);
os.close();
}
}
示例9: handleClientStart
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private void handleClientStart(HttpExchange t, ConsoleProxyClient viewer, String title, String guest) throws IOException {
List<String> languages = t.getRequestHeaders().get("Accept-Language");
String response = viewer.onAjaxClientStart(title, languages, guest);
Headers hds = t.getResponseHeaders();
hds.set("Content-Type", "text/html");
hds.set("Cache-Control", "no-cache");
hds.set("Cache-Control", "no-store");
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
try {
os.write(response.getBytes());
} finally {
os.close();
}
}
示例10: withoutEntityHeaders
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
@NotNull
public static Headers withoutEntityHeaders(@NotNull final HttpExchange httpExchange, @NotNull final String cacheControlHeaderValue)
{
final Headers responseHeaders = httpExchange.getResponseHeaders();
responseHeaders.set(ServerHeaderName, ServerHeaderValue);
responseHeaders.set(DateHeaderName, rfc2822DateForNow());
responseHeaders.set(ContentLanguageHeaderName, ContentLanguageHeaderValue);
responseHeaders.set(ConnectionHeaderName, "close");
final boolean isHttps = httpExchange instanceof HttpsExchange;
if (isHttps)
{
responseHeaders.set(StrictTransportSecurityHeaderName, StrictTransportSecurityHeaderValueMaximum);
}
responseHeaders.set(XFrameOptionsHeaderName, "DENY");
responseHeaders.set(XXssProtectionHeaderName, "1; mode=block");
responseHeaders.set(XRimAutoMatchHeaderName, "none");
responseHeaders.set(XRobotsTagHeaderName, "none");
responseHeaders.set(AccessControlAllowOriginHeaderName, "*");
responseHeaders.set(AccessControlAllowMethodsHeaderName, "POST, PUT, DELETE, GET, HEAD, OPTIONS");
responseHeaders.set(AccessControlAllowHeadersHeaderName, "Authorization, Content-Type");
responseHeaders.set(CacheControlHeaderName, cacheControlHeaderValue);
responseHeaders.set(AcceptRangesHeaderName, "none");
return responseHeaders;
}
开发者ID:health-and-care-developer-network,项目名称:health-and-care-developer-network,代码行数:25,代码来源:ResponseHeadersHelper.java
示例11: sendData
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private void sendData(String contentType, byte[] data, HttpExchange exchange) {
Headers outHeaders = exchange.getResponseHeaders();
outHeaders.set("Content-Type", contentType);
try {
exchange.sendResponseHeaders(200, data.length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(data);
outputStream.close();
} catch (IOException ex) {
sendHTMLError(500, "Server Error", exchange);
com.gmt2001.Console.err.println("HTTPS Server: sendData()" + ex.getMessage());
com.gmt2001.Console.err.logStackTrace(ex);
}
}
示例12: sendHTMLError
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private void sendHTMLError(int error, String message, HttpExchange exchange) {
Headers outHeaders = exchange.getResponseHeaders();
outHeaders.set("Context-Type", "text/text");
try {
exchange.sendResponseHeaders(error, message.length());
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(message.getBytes());
outputStream.close();
} catch (IOException ex) {
// Do not generate another HTML error, as we are already in sendHTMLError which failed.
com.gmt2001.Console.err.println("HTTPS Server: sendHTMLError()" + ex.getMessage());
com.gmt2001.Console.err.logStackTrace(ex);
}
}
示例13: getHttpHandler
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
private HttpHandler getHttpHandler() {
return new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
try {
final Headers headers = httpExchange.getResponseHeaders();
final String requestMethod = httpExchange.getRequestMethod().toUpperCase();
switch (requestMethod) {
case METHOD_GET:
final String responseBody = getUrlComponent(httpExchange.getRequestURI());
headers.set(HEADER_CONTENT_TYPE,
String.format("%s; charset=%s", TYPE.getMimeType(httpExchange.getRequestURI().toString()), CHARSET));
final byte[] rawResponseBody = responseBody.getBytes(CHARSET);
httpExchange.sendResponseHeaders(STATUS_OK, rawResponseBody.length);
httpExchange.getResponseBody().write(rawResponseBody);
break;
case METHOD_OPTIONS:
headers.set(HEADER_ALLOW, ALLOWED_METHODS);
httpExchange.sendResponseHeaders(STATUS_OK, NO_RESPONSE_LENGTH);
break;
default:
headers.set(HEADER_ALLOW, ALLOWED_METHODS);
httpExchange.sendResponseHeaders(STATUS_METHOD_NOT_ALLOWED, NO_RESPONSE_LENGTH);
break;
}
} finally {
httpExchange.close();
}
}
};
}
示例14: moved
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
void moved(HttpExchange t) throws IOException {
Headers req = t.getRequestHeaders();
Headers map = t.getResponseHeaders();
URI uri = t.getRequestURI();
String host = req.getFirst("Host");
String location = "http://" + host + uri.getPath() + "/";
map.set("Content-Type", "text/html");
map.set("Location", location);
t.sendResponseHeaders(301, -1);
t.close();
}
示例15: writeResponse
import com.sun.net.httpserver.Headers; //导入方法依赖的package包/类
void writeResponse(HttpExchange httpExchange, int code, String response) throws IOException {
Headers headers = httpExchange.getResponseHeaders();
headers.set("Content-Type", "application/json; charset=utf-8");
httpExchange.sendResponseHeaders(code, response.length());
try (OutputStream os = httpExchange.getResponseBody()) {
os.write(response.getBytes());
}
}