本文整理汇总了Java中com.sun.net.httpserver.HttpExchange.getRequestHeaders方法的典型用法代码示例。如果您正苦于以下问题:Java HttpExchange.getRequestHeaders方法的具体用法?Java HttpExchange.getRequestHeaders怎么用?Java HttpExchange.getRequestHeaders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.HttpExchange
的用法示例。
在下文中一共展示了HttpExchange.getRequestHeaders方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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 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();
}
示例4: fromExchange
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
public static WebRequest fromExchange(HttpExchange exchange) {
HttpMethod method = HttpMethod.valueOf(exchange.getRequestMethod());
return new WebRequest(exchange.getRequestURI().getPath(),
method,
Parameters.fromString(exchange.getRequestURI().getQuery()),
method == HttpMethod.POST ? Parameters.fromInputStream(exchange.getRequestBody()) : Parameters.empty(),
exchange.getRequestHeaders()
);
}
示例5: testPut
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
private void testPut(int status) throws MalformedURLException, ParseException {
//Util.enableHttpClientTracing();
int numDataPoints = 10;
ArrayList<DataPoint> dataPoints = createDataPoints(numDataPoints);
URL apiEndPoint = httpServer.getUrl(status);
ApptuitPutClient client = new ApptuitPutClient(MockServer.token, globalTags, apiEndPoint);
client.put(dataPoints);
List<HttpExchange> exchanges = httpServer.getExchanges();
List<String> requestBodies = httpServer.getRequestBodies();
HttpExchange exchange = exchanges.get(0);
assertEquals("POST", exchange.getRequestMethod());
assertEquals(MockServer.path, exchange.getRequestURI().getPath());
Headers headers = exchange.getRequestHeaders();
assertEquals("gzip", headers.getFirst("Content-Encoding"));
assertEquals("application/json", headers.getFirst("Content-Type"));
assertEquals("Bearer " + MockServer.token, headers.getFirst("Authorization"));
DataPoint[] unmarshalledDPs = Util.jsonToDataPoints(requestBodies.get(0));
assertEquals(numDataPoints, unmarshalledDPs.length);
for (int i = 0; i < numDataPoints; i++) {
assertEquals(getExpectedDataPoint(dataPoints.get(i), globalTags), unmarshalledDPs[i]);
}
}
示例6: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(HttpExchange event) throws IOException {
// Get the paramString form the request
String line;
headers = event.getRequestHeaders();
InputStream is = event.getRequestBody();
// initiating
BufferedReader in = new BufferedReader(new InputStreamReader(is));
stringBuilder = new StringBuilder();
while ((line = in.readLine()) != null) {
stringBuilder = stringBuilder.append(line).append("\n");
}
logger.info("Event Arrived: " + stringBuilder.toString());
isEventArrived.set(true);
}
示例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 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();
}
示例9: 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();
}
示例10: 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();
OutputStream os = t.getResponseBody();
URI uri = t.getRequestURI();
String path = uri.getPath();
while (is.read() != -1) ;
is.close();
File f = new File(_docroot, path);
if (!f.exists()) {
notfound(t, path);
return;
}
String method = t.getRequestMethod();
if (method.equals("HEAD")) {
rmap.set("Content-Length", Long.toString(f.length()));
t.sendResponseHeaders(200, -1);
t.close();
} else if (!method.equals("GET")) {
t.sendResponseHeaders(405, -1);
t.close();
return;
}
if (path.endsWith(".html") || path.endsWith(".htm")) {
rmap.set("Content-Type", "text/html");
} else {
rmap.set("Content-Type", "text/plain");
}
t.sendResponseHeaders (200, f.length());
FileInputStream fis = new FileInputStream(f);
int count = 0;
try {
byte[] buf = new byte[16 * 1024];
int len;
while ((len = fis.read(buf)) != -1) {
os.write(buf, 0, len);
count += len;
}
} catch (IOException e) {
e.printStackTrace();
}
fis.close();
os.close();
}