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

  • SEARCH
  • Home
  • 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 4021476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:21:23+00:00 2026-05-20T10:21:23+00:00

The client: public List<String> post(List<String> toWrite){ String result = ; List<String> allResults = new

  • 0

The client:

    public List<String> post(List<String> toWrite){
        String result = "";
        List<String> allResults = new ArrayList<String>();

        try {
            openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            initializeOutputStream();
            for(int i = 0; i < toWrite.size(); i++){
                out.write(toWrite.get(i));
                out.newLine();
            }
            System.out.println(connection.getResponseCode());
            System.out.println(connection.getResponseMessage());
            initializeInputStream();
            while((result = in.readLine()) != null){
                allResults.add(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }

        return allResults;
    }

One of the attempts at the host:

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        List<String> incoming = new ArrayList<String>();

//      BufferedReader in = req.getReader();
//
//      String tmp = in.readLine();

        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();

        StringBuilder stringBuilder = new StringBuilder();  
        BufferedReader bufferedReader = null;  
        try {  
          InputStream inputStream = req.getInputStream();  
          if (inputStream != null) {  
           bufferedReader = new BufferedReader(new InputStreamReader(  
        inputStream));  
           char[] charBuffer = new char[128];  
           int bytesRead = -1;  
           //while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
           while(bytesRead != -1){
            stringBuilder.append(charBuffer, 0, bytesRead);  
           }  
          } else {  
           stringBuilder.append("");  
          }  
        } catch (IOException ex) {  
          throw ex;  
        } finally {  
          if (bufferedReader != null) {  
           try {  
            bufferedReader.close();  
           } catch (IOException ex) {  
            throw ex;  
           }  
          }  
        }  
        String body = stringBuilder.toString();  
        System.out.println(body);

        out.println(body);

//      BufferedReader in = new BufferedReader(new InputStreamReader(req.getInputStream()));        
//      
//      String tmp = "";
//      
//      //while(!(in.ready())){}
//      
//      while((tmp = in.readLine()) != null){
//          System.out.println(tmp);
//      }
//
//
//      out.println(tmp);

        out.println("end");

    }

Please note the commented out lines- thats one of the many other attempts I’ve tried to get stuff from the client.

System.out.printlns and out.printlns from the servlett all return a blank line. The “end” at the end of the program returns without problem. It is not a problem of reading multiple lines back on the client side- if I put multiple out.println’s, then I read them fine. The system.out.println() for the inputstream also returns blank. The status code is 200, so there seems to be no connection errors.

Anyone?

  • 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-20T10:21:24+00:00Added an answer on May 20, 2026 at 10:21 am

    It seems like your while loop doesn’t do anything here, because in your case, bytesRead is always -1 thus it will never get into the loop at all, and further, you don’t use your bufferedReader at all to read from the input stream:-

        int bytesRead = -1;
        while (bytesRead != -1) {
            stringBuilder.append(charBuffer, 0, bytesRead);
        }
    

    Try this:-

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();
    
        StringBuilder stringBuilder = new StringBuilder(1000);
        Scanner scanner = new Scanner(req.getInputStream());
        while (scanner.hasNextLine()) {
            stringBuilder.append(scanner.nextLine());
        }
    
        String body = stringBuilder.toString();
    
        System.out.println(body);
        out.println(body);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public IList GetClientsByListofID(IList ids) where T : IClient { IList clients = new List();
I am trying to use the following method: public static String UrlToString(String targetURL, List
I have a class Client like that: public class Client { public Person Pers
I have a Client class like that: public class Client { public Person Pers
I have a client certificate but I am unable to obtain the public key
I'm working with a client to plan the topology for a public facing SharePoint
I have a class with following description: public class Customer { public ISet<Client> Contacts
I'm looking for a good open source Windows FTP client library with a public
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
Data sent from client will not get deserialized. Client: $.ajax({ type: 'POST', dataType: json,

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.