write a string from URLConnection to a servlet

D

dave

I'm trying to write a string from URLConnection to a servlet. what I'm
trying to do is, get an XML file from client over URL so that the
servlet can get it and process it and send the response to the client.
The problem, here is I'm not getting the XMLstring to the servlet. I
do the
following steps:

*client side*
1. create URL corresponding to the servlet
2. get URLConnection ( url.openConnection() )
3. enable output ( connection.setDoOutput(true) )
4. get output stream ( connection.getOutputStream() )
5. wrap the output stream into PrintWriter
6. println(string)
7. flush/close output stream

*server side*


1. get input stream for the servlet ( request.getInputStream() )
2. wrap it to BufferedReader
3. read the string
4. process it using SAX Parser
5. send the response to the client
It looks like the string has not come
through to the servlet.

for details:
*********servlet side*********************
reader = new BufferedReader(new
InputStreamReader(httpRequest.getInputStream()));


String line = null;
int letter = 0;
while ((letter = reader.read()) != -1) {
buffer.append(letter);
//System.out.println(line);
}
} catch (Exception e) {
log(ERROR,"Cannot read from URL" + e.toString());
throw e;
} finally {
try {
reader.close();
} catch (IOException io) {
log(ERROR,"Error closing URLReader!");
throw io;
}
}

System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
SaxParser sp= new SaxParser(buffer.toString(), true);

**************client Side***********************
URL url = new URL("http://localhost/lkn?action=cartToCart");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();


connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);

String queryString = buildXml().toString();



OutputStream out = connection.getOutputStream();
out.write(queryString.getBytes());

//out.write(queryString.getBytes());
out.flush();
out.close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top