本文整理汇总了Java中com.sun.net.httpserver.HttpExchange.sendResponseHeaders方法的典型用法代码示例。如果您正苦于以下问题:Java HttpExchange.sendResponseHeaders方法的具体用法?Java HttpExchange.sendResponseHeaders怎么用?Java HttpExchange.sendResponseHeaders使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.HttpExchange
的用法示例。
在下文中一共展示了HttpExchange.sendResponseHeaders方法的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 {
URI uri = t.getRequestURI();
String query = uri.getQuery();
if ("kill".equals(query)) {
// a kill request will stop the server
System.exit(0);
}
else if (query==null || query.length()==0 || "test".equals(query)) {
// an empty request or a test request will return an empty page;
// this is used to check whether the server is running
t.sendResponseHeaders(200, 0);
OutputStream os = t.getResponseBody();
os.write("Connected!".getBytes(Charset.forName("UTF-8")));
os.close();
}
}
示例3: 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();
}
}
示例4: 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 map = t.getResponseHeaders();
String redirect = root + "/foo/" + Integer.toString(count);
increment();
map.add("Location", redirect);
t.sendResponseHeaders(301, -1);
t.close();
}
示例5: 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();
}
示例6: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
while (is.read() != -1);
is.close();
t.sendResponseHeaders (200, 0);
OutputStream os = t.getResponseBody();
DigestOutputStream dos = new DigestOutputStream(os, serverDigest);
int offset = 0;
for (int i=0; i<52; i++) {
dos.write(baMessage, offset, CHUNK_SIZE);
offset += CHUNK_SIZE;
}
serverMac = serverDigest.digest();
os.close();
t.close();
}
示例7: 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();
}
示例8: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange t) throws IOException {
t.sendResponseHeaders(200, message.length());
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(t.getResponseBody(), "ISO8859-1"));
writer.write(message, 0, message.length());
writer.close();
t.close();
}
示例9: okReply
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
static void okReply (HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().add("Connection", "close");
String response = "Hello .";
exchange.sendResponseHeaders(200, response.getBytes().length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes());
}
exchange.close();
}
示例10: respondRedirect
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
private static void respondRedirect(HttpExchange httpExchange) throws IOException {
byte[] respBody = "Metrics are provided on the /metrics endpoint.".getBytes("UTF-8");
httpExchange.getResponseHeaders().add("Location", "/metrics");
httpExchange.getResponseHeaders().put("Context-Type", Collections.singletonList("text/plain; charset=UTF-8"));
httpExchange.sendResponseHeaders(302, respBody.length);
httpExchange.getResponseBody().write(respBody);
httpExchange.getResponseBody().close();
}
示例11: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange httpExchange) throws IOException {
if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) {
if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeoutMillis + "ms")) {
String nodesInfoBody = sniffResponse.nodesInfoBody;
httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length());
try (OutputStream out = httpExchange.getResponseBody()) {
out.write(nodesInfoBody.getBytes(Consts.UTF_8));
return;
}
}
}
httpExchange.sendResponseHeaders(404, 0);
httpExchange.close();
}
示例12: 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);
}
}
示例13: 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();
}
示例14: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
public void handle (HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
while (is.read () != -1) ;
is.close();
t.sendResponseHeaders(200, -1);
HttpPrincipal p = t.getPrincipal();
if (!p.getUsername().equals(USERNAME)) {
error = true;
}
if (!p.getRealm().equals(REALM)) {
error = true;
}
t.close();
}
示例15: sendOk
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
public static void sendOk(HttpExchange event) throws IOException {
try {
event.sendResponseHeaders(200, 0);
} finally {
event.close();
}
}