Java ApacheHttpCore,一个简单的服务器,用于回送接收到的post数据
使用此处找到的ElementalHttpServer示例类: 我能够成功地接收post数据,我的目标是将接收到的post数据转换为可以打印的字符串。我对HttpFileHandler进行了如下修改,使用eneity.getContent()获取inputStream,但我不确定如何将inputStream转换为字符串Java ApacheHttpCore,一个简单的服务器,用于回送接收到的post数据,java,http,post,apache-httpcomponents,Java,Http,Post,Apache Httpcomponents,使用此处找到的ElementalHttpServer示例类: 我能够成功地接收post数据,我的目标是将接收到的post数据转换为可以打印的字符串。我对HttpFileHandler进行了如下修改,使用eneity.getContent()获取inputStream,但我不确定如何将inputStream转换为字符串 static class HttpFileHandler implements HttpRequestHandler { private final String doc
static class HttpFileHandler implements HttpRequestHandler {
private final String docRoot;
public HttpFileHandler(final String docRoot) {
super();
this.docRoot = docRoot;
}
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
throw new MethodNotSupportedException(method + " method not supported");
}
String target = request.getRequestLine().getUri();
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
byte[] entityContent = EntityUtils.toByteArray(entity);
InputStream inputStream = entity.getContent();
String str= inputStream.toString();
byte[] b3=str.getBytes();
String st = new String(b3);
System.out.println(st);
for(int i=0;i<b3.length;i++) {
System.out.print(b3[i]+"\t");
}
System.out.println("Incoming entity content (bytes): " + entityContent.length);
}
}
静态类HttpFileHandler实现HttpRequestHandler{
私有最终字符串docRoot;
公共HttpFileHandler(最终字符串docRoot){
超级();
this.docRoot=docRoot;
}
公共无效句柄(
最终HttpRequest请求,
最终HttpResponse响应,
最终HttpContext上下文)引发HttpException,IOException{
String method=request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
如果(!method.equals(“GET”)和&!method.equals(“HEAD”)和&!method.equals(“POST”)){
抛出新MethodNotSupportedException(方法+“不支持的方法”);
}
String target=request.getRequestLine().getUri();
if(HttpEntityEnclosingRequest的请求实例){
HttpEntity实体=((HttpEntityEnclosingRequest)请求).getEntity();
字节[]entityContent=EntityUtils.toByteArray(实体);
InputStream InputStream=entity.getContent();
String str=inputStream.toString();
byte[]b3=str.getBytes();
字符串st=新字符串(b3);
系统输出打印LN(st);
对于(int i=0;i,这里有一个简单的控制台日志记录处理程序;它记录每个请求(不仅仅是POST)-包括头和负载:
package com.mycompany;
import org.apache.http.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;
import org.omg.CORBA.Request;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by IntelliJ IDEA.
* User: Piotrek
* To change this template use File | Settings | File Templates.
*/
public class LoggingHandler implements HttpRequestHandler {
public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
System.out.println(""); // empty line before each request
System.out.println(httpRequest.getRequestLine());
System.out.println("-------- HEADERS --------");
for(Header header: httpRequest.getAllHeaders()) {
System.out.println(header.getName() + " : " + header.getValue());
}
System.out.println("--------");
HttpEntity entity = null;
if (httpRequest instanceof HttpEntityEnclosingRequest)
entity = ((HttpEntityEnclosingRequest)httpRequest).getEntity();
// For some reason, just putting the incoming entity into
// the response will not work. We have to buffer the message.
byte[] data;
if (entity == null) {
data = new byte [0];
} else {
data = EntityUtils.toByteArray(entity);
}
System.out.println(new String(data));
httpResponse.setEntity(new StringEntity("dummy response"));
}
}
使用org.apache.http.localserver.LocalTestServer
注册处理程序(与ElementalHttpServer
类似-上面还有HttpRequestHandler
实现):
public static void main(String[] args) throws Exception {
LocalTestServer server = new LocalTestServer(null, null);
try {
server.start();
server.register("/*", new LoggingHandler());
server.awaitTermination(3600 * 1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
server.stop();
}
}