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


Java HttpExchange.sendResponseHeaders方法代码示例

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

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

示例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();
	}
}
 
开发者ID:Bort-Millipede,项目名称:WLT3Serial,代码行数:27,代码来源:WebServerTestHelper.java

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

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

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

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

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

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

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

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

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

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

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

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


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