正在使用 HTTPServer
编写服务由com.sun.net.httpserver.HttpServer
提供包裹。我需要将一些相当大的数据作为字节流发送到该服务(比如一百万个整数)。
我几乎搜索了所有可用的示例,都指向发送一个小GET
请求在 URL 中。
我需要知道如何将数据发送为 POST
要求。
另外,可以发送的数据有限制吗?
最佳答案
我建议使用 Apache HTTPClient 库(如果您不使用 Spring 进行 applet-servlet 通信),而不是上面答案中的持久 URL 连接
http://hc.apache.org/httpclient-3.x/
在此,您可以构建一个客户端并通过 HTTP 将序列化对象(例如序列化为 JSON 字符串: https://code.google.com/p/google-gson/ )作为 POST 请求发送到您的服务器:
public HttpResponse sendStuff (args) {
HttpPost post = null;
try {
HttpClient client = HttpClientBuilder.create().build();
post = new HttpPost(servletUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(<nameString>, <valueString>));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
response.getStatusLine().getStatusCode();
return response;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
但是 Spring 可以为您节省大量时间和麻烦,因此我会推荐 check it out
关于java - HTTPServer 发布参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355138/