Question

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.

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top