当前位置: 首页>>代码示例>>Java>>正文


Java HttpExchange.getRequestHeaders方法代码示例

本文整理汇总了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();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:25,代码来源:RestClientSingleHostIntegTests.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:HttpOnly.java

示例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();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:20,代码来源:Deadlock.java

示例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()
    );
}
 
开发者ID:pierrelemee,项目名称:potato,代码行数:11,代码来源:WebRequest.java

示例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]);
  }
}
 
开发者ID:ApptuitAI,项目名称:JInsight,代码行数:30,代码来源:ApptuitPutClientTest.java

示例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);
}
 
开发者ID:wso2-extensions,项目名称:siddhi-io-http,代码行数:16,代码来源:HttpServerListener.java

示例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();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:40,代码来源:Basic.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:VersionTest.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:SimpleHttpServer.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:54,代码来源:SimpleHttpServer.java


注:本文中的com.sun.net.httpserver.HttpExchange.getRequestHeaders方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。