本文整理汇总了Java中com.sun.net.httpserver.HttpExchange.getResponseBody方法的典型用法代码示例。如果您正苦于以下问题:Java HttpExchange.getResponseBody方法的具体用法?Java HttpExchange.getResponseBody怎么用?Java HttpExchange.getResponseBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.HttpExchange
的用法示例。
在下文中一共展示了HttpExchange.getResponseBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
}
示例2: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange t) throws IOException {
// retrieve the request json data
InputStream is = t.getRequestBody();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int len;
while ((len = is.read(buffer))>0) {
bos.write(buffer, 0, len);
}
bos.close();
String data = new String(bos.toByteArray());
System.out.println("Request: \n " + data);
// pass the data to the handler and receive a response
String response = handleRequest(data);
System.out.println(" " + response);
// format and return the response to the user
t.sendResponseHeaders(200, response.getBytes(Charset.forName("UTF-8")).length);
t.getResponseHeaders().set("Content-Type", "application/json, charset=UTF-8");
OutputStream os = t.getResponseBody();
os.write(response.getBytes(Charset.forName("UTF-8")));
os.close();
}
示例3: 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();
}
示例4: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
public void handle(HttpExchange exchange) throws IOException {
String requestMethod = exchange.getRequestMethod();
System.out.println(requestMethod + " /post");
if (requestMethod.equalsIgnoreCase("POST")) {
String body = new BufferedReader(
new InputStreamReader(
exchange.getRequestBody()
)
).lines().collect(Collectors.joining("\n"));
String[] parts = body.split("=");
String name = null;
if (parts.length > 1) {
name = parts[1];
}
exchange.getResponseHeaders().set(Constants.CONTENTTYPE, Constants.TEXTHTML);
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
responseBody.write(Constants.getIndexHTML(name));
responseBody.close();
} else {
new NotImplementedHandler().handle(exchange);
}
}
示例5: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange he) throws IOException {
String method = he.getRequestMethod();
InputStream is = he.getRequestBody();
if (method.equalsIgnoreCase("POST")) {
String requestBody = new String(is.readAllBytes(), US_ASCII);
if (!requestBody.equals(POST_BODY)) {
he.sendResponseHeaders(500, -1);
ok = false;
} else {
he.sendResponseHeaders(200, -1);
ok = true;
}
} else { // GET
he.sendResponseHeaders(200, RESPONSE.length());
OutputStream os = he.getResponseBody();
os.write(RESPONSE.getBytes(US_ASCII));
os.close();
ok = true;
}
}
示例6: 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();
}
示例7: 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();
}
示例8: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange t) throws IOException {
String response = "Hello from Gradle!";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
示例9: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public synchronized void handle(HttpExchange t)
throws IOException
{
String reply = "Hello world";
int len = reply.length();
Headers h = t.getRequestHeaders();
checkHeader(h);
System.out.printf("Sending response 200\n");
t.sendResponseHeaders(200, len);
OutputStream o = t.getResponseBody();
o.write(reply.getBytes());
t.close();
}
示例10: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange t) throws IOException {
String response = "<table>" +
"<tr>" + "<td>hello</td> <td>A</td> <td>B</td>" + "</tr>" +
"<tr>" + "<td>0</td> <td>1</td> <td>B0</td>" + "</tr>" +
"</table>";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
示例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包/类
@Override
public void handle(HttpExchange r) throws IOException {
String response = "[{\"scheme\":\"http\", \"host\":\"localhost:15999\"}]";
r.sendResponseHeaders(200, response.length());
try (OutputStream os = r.getResponseBody()) {
os.write(response.getBytes());
}
}
示例13: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
static void handle(HttpExchange conn, String template, Object... params) throws IOException {
String content = String.format(template, params);
byte[] response = new byte[0];
try {
response = ("<!-- " + signature(content) + " -->\n" + content).getBytes("UTF-8");
} catch (Exception e) {
throw new IOException(e.getMessage());
}
conn.getResponseHeaders().add("Content-Type", "text/xml");
conn.sendResponseHeaders(200, response.length);
OutputStream os = conn.getResponseBody();
os.write(response);
os.close();
}
示例14: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
byte[] ba = new byte[8192];
while(is.read(ba) != -1);
t.sendResponseHeaders(200, RESP_LENGTH);
try (OutputStream os = t.getResponseBody()) {
os.write(new byte[RESP_LENGTH]);
}
t.close();
}
示例15: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange httpExchange) {
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
if (parent.graphResult == null && parent.tupleResultBuilder == null) {
fail("Please specify either a graph or tuple result for your stub.");
}
if (parent.graphResult != null) {
httpExchange.getResponseHeaders().add("Content-Type", MediaTypes.TURTLE);
Rio.write(parent.graphResult, output, RDFFormat.TURTLE);
} else {
httpExchange.getResponseHeaders().add("Content-Type", MediaTypes.SPARQL_RESULTS_JSON);
TupleQueryResultWriter writer = new SPARQLResultsJSONWriter(output);
QueryResults.report(parent.tupleResultBuilder.build(), writer);
}
httpExchange.sendResponseHeaders(200, output.size());
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write(output.toByteArray());
responseBody.close();
} catch (Exception ex) {
fail(ex.getMessage());
}
}