在java中的servert_在Java HttpServer响应中设置内容类型

我有一个java echo httpserver了.

它适用于测试站点,但我正在使用的客户端代码无法获取数据.

当两者合并时,我得到一个状态代码为200的响应,但是即使它被发送,客户端也不会显示元数据/正文内容.

我唯一的猜测是因为内容类型不包括在内,客户可能会对此感到挑剔.

如何在响应中指定内容类型?

(注意,客户端将一个字符串发送到服务器,并将POST作为参数以及一些标题信息.此代码目前设置为仅返回正文内容/参数.)

任何想法赞赏!

import static java.net.HttpURLConnection.HTTP_OK;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.InetSocketAddress;

import java.net.URLDecoder;

import java.util.List;

import com.sun.net.httpserver.Headers;

import com.sun.net.httpserver.HttpExchange;

import com.sun.net.httpserver.HttpHandler;

import com.sun.net.httpserver.HttpServer;

/**

* Echo the body of an HTTP request back as the HTTP response. This is merely

* a simple exercise of the Secret Sun Web Server. As configured, the URL to

* access it is http://localhost:8000/echo.

*

* @author Andrew Cowie

*/

public final class Test

{

public static void main(String[] args) throws IOException {

final InetSocketAddress addr;

final HttpServer server;

addr = new InetSocketAddress(8000);

server = HttpServer.create(addr, 10);

server.createContext("/echo", new EchoHandler());

server.start();

}

}

class EchoHandler implements HttpHandler

{

public void handle(HttpExchange t) throws IOException {

final InputStream is;

final OutputStream os;

StringBuilder buf;

int b;

final String request, response;

buf = new StringBuilder();

/*

* Get the request body and decode it. Regardless of what you are

* actually doing, it is apparently considered correct form to consume

* all the bytes from the InputStream. If you don't, closing the

* OutputStream will cause that to occur

*/

is = t.getRequestBody();

while ((b = is.read()) != -1) {

buf.append((char) b);

}

is.close();

if (buf.length() > 0) {

request = URLDecoder.decode(buf.toString(), "UTF-8");

} else {

request = null;

}

/*

* Construct our response:

*/

buf = new StringBuilder();

//buf.append("

HTTP echo server");

//buf.append("

");

//buf.append(t.getRequestMethod() + " " + t.getRequestURI() + " " + t.getProtocol() + "\n");

/*

* Process the request headers. This is a bit involved due to the

* complexity arising from the fact that headers can be repeated.

*/

Headers headers = t.getRequestHeaders();

for (String name : headers.keySet()) {

List values = headers.get(name);

for (String value : values) {

//buf.append(name + ": " + value + "\n");

}

}

/*

* If there was an actual body to the request, add it:

*/

if (request != null) {

//buf.append("\n");

buf.append(request);

}

//buf.append("

");

//buf.append("\n");

response = buf.toString();

System.out.println(response);

/*

* And now send the response. We could have instead done this

* dynamically, using 0 as the response size (forcing chunked

* encoding) and writing the bytes of the response directly to the

* OutputStream, but building the String first allows us to know the

* exact length so we can send a response with a known size. Better :)

*/

t.sendResponseHeaders(HTTP_OK, response.length());

os = t.getResponseBody();

os.write(response.getBytes());

/*

* And we're done!

*/

os.close();

t.close();

}

}

解决方法:

尝试添加

t.getResponseHeaders().put("Content-Type", "text/html");

在写之前

标签:java,outputstream,server,httpserver

来源: https://codeday.me/bug/20190628/1310509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值