本文整理汇总了Java中com.sun.net.httpserver.HttpExchange.getResponseHeaders方法的典型用法代码示例。如果您正苦于以下问题:Java HttpExchange.getResponseHeaders方法的具体用法?Java HttpExchange.getResponseHeaders怎么用?Java HttpExchange.getResponseHeaders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.HttpExchange
的用法示例。
在下文中一共展示了HttpExchange.getResponseHeaders方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的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.HttpExchange; //导入方法依赖的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: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange exchange) throws IOException {
InputStream inputStream = exchange.getRequestBody();
String query;
String response;
try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
query = scanner.useDelimiter("\\A").next();
logger.info("Query: " + query);
inputStream.close();
response = queryServer(query, ReturnType.JSON);
} catch (Exception e) {
logger.info("ERROR", e);
response = new Message("ERROR: " + e.getMessage(), true).toJson().toString();
}
// Add the header to avoid error:
// “No 'Access-Control-Allow-Origin' header is present on the requested resource”
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.add("Access-Control-Allow-Origin", "*");
exchange.sendResponseHeaders(200, response.length());
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(response.getBytes());
outputStream.close();
}
示例4: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle (HttpExchange t)
throws IOException
{
InputStream is = t.getRequestBody();
Headers map = t.getRequestHeaders();
Headers rmap = t.getResponseHeaders();
while (is.read() != -1);
is.close();
t.sendResponseHeaders(200, -1);
HttpPrincipal p = t.getPrincipal();
if (!p.getUsername().equals("fred")) {
error = true;
}
if (!p.getRealm().equals("[email protected]")) {
error = true;
}
t.close();
}
示例5: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
public void handle(HttpExchange exchange) {
try {
exchange.getRequestMethod();
exchange.getRequestBody().close();
Headers respHeaders = null;
if(!accessed && (source == null)) { //context hasn't been accessed before
source = exchange.getRemoteAddress().getHostString();
accessed = true;
respHeaders = exchange.getResponseHeaders();
respHeaders.add("Content-Type","text/plain");
exchange.sendResponseHeaders(200,path.length());
OutputStream os = exchange.getResponseBody();
os.write(path.getBytes());
os.flush();
os.close();
accessList.put(path,source);
} else {
respHeaders = exchange.getResponseHeaders();
exchange.sendResponseHeaders(404,0);
exchange.getResponseBody().close();
}
} catch(Exception e) {
System.err.println("Unforeseen error occurred!");
e.printStackTrace();
}
}
示例6: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public synchronized void handle(HttpExchange t)
throws IOException {
byte[] buf = new byte[2048];
System.out.println("Server: " + t.getRequestURI());
try (InputStream is = t.getRequestBody()) {
while (is.read(buf) != -1) ;
}
increment();
if (count() == 1) {
Headers map = t.getResponseHeaders();
String redirect = "/redirect/bar.txt";
map.add("Location", redirect);
t.sendResponseHeaders(301, -1);
t.close();
} else {
String response = "Hello world";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes(StandardCharsets.ISO_8859_1));
t.close();
}
}
示例7: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public synchronized void handle(HttpExchange t)
throws IOException
{
byte[] buf = new byte[2048];
try (InputStream is = t.getRequestBody()) {
while (is.read(buf) != -1) ;
}
Headers responseHeaders = t.getResponseHeaders();
if (count++ < 1) {
responseHeaders.add("Location", root + "/foo/" + count);
} else {
responseHeaders.add("Location", SmokeTest.midSizedFilename);
}
t.sendResponseHeaders(301, -1);
t.close();
}
示例8: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange he) throws IOException {
List<String> l = he.getRequestHeaders().get("X-Bar");
if (!l.contains("foo1") || !l.contains("foo2")) {
for (String s : l) {
System.out.println("HH: " + s);
}
he.sendResponseHeaders(500, -1);
he.close();
return;
}
Headers h = he.getResponseHeaders();
h.add("X-Foo-Response", "resp1");
h.add("X-Foo-Response", "resp2");
he.sendResponseHeaders(200, RESPONSE.length());
OutputStream os = he.getResponseBody();
os.write(RESPONSE.getBytes(US_ASCII));
os.close();
}
示例9: CORS
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
private static boolean CORS(HttpExchange httpExchange) throws IOException {
Headers headers = httpExchange.getResponseHeaders();
headers.add("Access-Control-Allow-Headers", "Content-Type");
headers.add("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE");
headers.add("Access-Control-Allow-Origin", httpExchange.getRequestHeaders().getFirst("origin"));
headers.add("Access-Control-Max-Age", "3600");
if (!httpExchange.getRequestMethod().equals("OPTIONS")) return false;
httpExchange.sendResponseHeaders(200, 0);
write(httpExchange, "");
return true;
}
示例10: sendData
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的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);
}
}
示例11: sendHTMLError
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的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);
}
}
示例12: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
Headers map = t.getRequestHeaders();
Headers rmap = t.getResponseHeaders();
URI uri = t.getRequestURI();
debug("Server: received request for " + uri);
String path = uri.getPath();
if (path.endsWith("a.jar"))
aDotJar++;
else if (path.endsWith("b.jar"))
bDotJar++;
else if (path.endsWith("c.jar"))
cDotJar++;
else
System.out.println("Unexpected resource request" + path);
while (is.read() != -1);
is.close();
File file = new File(docsDir, path);
if (!file.exists())
throw new RuntimeException("Error: request for " + file);
long clen = file.length();
t.sendResponseHeaders (200, clen);
OutputStream os = t.getResponseBody();
FileInputStream fis = new FileInputStream(file);
try {
byte[] buf = new byte [16 * 1024];
int len;
while ((len=fis.read(buf)) != -1) {
os.write (buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
fis.close();
os.close();
}
示例13: getHttpHandler
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的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: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange he) throws IOException {
String method = he.getRequestMethod();
InputStream is = he.getRequestBody();
Headers h = he.getResponseHeaders();
h.add("X-Foo-Response", "resp");
he.sendResponseHeaders(200, RESPONSE.length());
OutputStream os = he.getResponseBody();
os.write(RESPONSE.getBytes(US_ASCII));
os.close();
}
示例15: moved
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的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();
}