Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:48:54+00:00 2026-05-25T00:48:54+00:00

Using the ElementalHttpServer example class found here: https://hc.apache.org/httpcomponents-core-4.3.x/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java I am able to successfully receive

  • 0

Using the ElementalHttpServer example class found here:

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

I am able to successfully receive post data, my goal is to convert the received post data into a string I can print. I’ve modified the HttpFileHandler as follows, using eneity.getContent() to get the inputStream, but i’m not sure how I can convert the inputStream into a String.

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

}

Thanks for any ideas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:48:55+00:00Added an answer on May 25, 2026 at 12:48 am

    Here is simple console logging handler; it logs every request (not only POST) – both headers and payload:

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

    Registration of handler using org.apache.http.localserver.LocalTestServer (with ElementalHttpServer it is similar – you also have HttpRequestHandler implementation above):

     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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C#, I need a class called User that has a username, password, active
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using Core Data. Let's say we have models for Team and Player. Assume: -Each
Using a site like StackOverflow as an example: I have a table of users,
Has anyone got EclipseLink MOXy (I'm using eclipselink 2.1.0) to work with Java 5?
Using Java (1.6) is it better to call the clear() method on a List
Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter
Using Java, how can I extract all the links from a given web page?
using Core Data, how would I list (i.e. return an NSArray of NSStrings) all
Using online interfaces to a version control system is a nice way to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.