2011-08-26 14 views
8

ElementalHttpServer उदाहरण वर्ग का उपयोग करना प्रतिध्वनित करने के लिए सरल सर्वर मिली:Apache httpcore, प्राप्त पोस्ट डेटा

https://hc.apache.org/httpcomponents-core-4.3.x/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java

मैं सफलतापूर्वक पोस्ट डेटा प्राप्त करने में सक्षम हूँ, मेरा लक्ष्य एक में प्राप्त पद डेटा कन्वर्ट करने के लिए है स्ट्रिंग मैं प्रिंट कर सकते हैं। मैंने इनपुटस्ट्रीम प्राप्त करने के लिए eneity.getContent() का उपयोग करके HttpFileHandler को निम्नानुसार संशोधित किया है, लेकिन मुझे यकीन नहीं है कि मैं इनपुटस्ट्रीम को स्ट्रिंग में कैसे परिवर्तित कर सकता हूं।

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); 
    } 
} 

}

किसी भी विचार के लिए धन्यवाद

उत्तर

10

यहाँ सरल सांत्वना प्रवेश हैंडलर है;

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(); 
    } 

} 
+0

काम करता है यही कारण है कि दोनों हेडर और पेलोड - यह हर अनुरोध (न केवल पोस्ट) लॉग पूरी तरह से, बहुत बहुत धन्यवाद !! – Hoofamon

+0

आपका स्वागत है :) –

संबंधित मुद्दे