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 9249721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:16:10+00:00 2026-06-18T10:16:10+00:00

I trying to send POST data with DataOutputStream and get response data. I coded

  • 0

I trying to send POST data with DataOutputStream and get response data.

I coded like this.

    String urlParameters = "table=page&format=xml";

    out.println(urlParameters+"<br/><br/><br/>");

    String searchUrl = "http://localhost:8081/WebTest/test.jsp";
    URL url = new URL(searchUrl); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Content-Length", ""+Integer.toString(urlParameters.getBytes().length));
    connection.setUseCaches (false);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeUTF(urlParameters);
    wr.flush();
    wr.close();

    if( connection.getResponseCode() == 200){

        XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
        XMLStreamReader reader = xmlFactory.createXMLStreamReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));

        try{
            while (reader.hasNext()) {
                Integer eventType = reader.next();
                if (eventType.equals(XMLStreamReader.START_ELEMENT)){
                    out.print(" " + reader.getName() + "<br/>");
                }else if (eventType.equals(XMLStreamReader.CHARACTERS)){
                    out.print(" " + reader.getText() + "<br/>");
                }else if (eventType.equals(XMLStreamReader.ATTRIBUTE)){
                    out.print(" " + reader.getName() + " <br/>");
                }else if (eventType.equals(XMLStreamReader.END_ELEMENT)){
                    out.print(" " + reader.getName() + " <br/>");
                }
            }
        } catch (XMLStreamException e){
            e.printStackTrace();
        } finally{

            connection.disconnect();
            reader.close();
        }
    }

and this is test.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page  language="java" contentType="text/xml;  charset=UTF-8" %> 
<response>
  <table><%=request.getParameter("table") %></table>
</response>

But, result was

response

table
null
table

response

Why request.getParameter(“table”) cannot get (or DataOutputStream doesn’t send) data?

I’m so confused.

Thanks everyone’s help.

  • 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-06-18T10:16:11+00:00Added an answer on June 18, 2026 at 10:16 am

    You should not use DataOutputStream.writeUTF, see API

    First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for the character. If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.

    That is, what DataOutputStream.writeUTF writes can be read only with DataInputStream.readUTF

    I suggest to use

        OutputStreamWriter w = new OutputStreamWriter(connection.getOuputStream(), "UTF-8");
        w.write(urlParameters);
        w.flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send POST data that is 2 million characters big (non-binary string)
I'm trying to send POST data to the current tab, based on this (
I am trying to send data to a web server using a post, this
I am trying to send the POST data from java to a PHP page.
I'm trying to send post data between pages with Post. Not a form -
I'm trying to send a post request to Parse: https://parse.com/docs/rest#objects-creating . Can't get it
I am trying to send data through post method in android . But how
I am trying to send form data to the current page for variable handling
I am trying to send a POST request to Drupal's Services module & JSON_Server
I m trying to send a url string using ajax post but dont not

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.