Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Sockets and Internet Protocols and the fly likes string getting converted to ASCII Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "string getting converted to ASCII" Watch "string getting converted to ASCII" New topic

string getting converted to ASCII

Reddy Anoop

Joined: Aug 22, 2007
Posts: 8

I am calling a servlet by passing XML data.

Sample xml that I am passing:

data1 =
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TestServlet SYSTEM "">
<TestConfig generatedDate="31 Oct 2007 16:27:30 GMT" dtdVersion="200703v1" qualification="false">
<MemoryModule mfgPartNum="501-6242" displaySequence="1000"/>

String line = URLEncoder.encode("data", "UTF-8") + "=" + URLEncoder.encode(data1, "UTF-8");

when I print the variable line, it prints ASCII values.

I wanted to send the xml as it is, can anyone please advise how to do this.


[ UD: added linebreak ]
[ November 14, 2007: Message edited by: Ulf Dittmer ]
Joe Ess

Joined: Oct 29, 2001
Posts: 9077

What do you suppose URLEncoder.encode does?

[How To Ask Questions On JavaRanch]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42954
It looks as if you are trying to send it as a GET request (otherwise there would be no need to use URLEncoder.encode). Is there a particular reason you don't want to use POST?
Reddy Anoop

Joined: Aug 22, 2007
Posts: 8
I am using POST. Here is the code that I have. The servlet accepts a varible called "data" that has the complete xml. Please suggest.

String str = "";
String line = "";
String data1 = "";
// Send data
URL url = new URL(" http://xx123.test:80/translate");
URLConnection conn = url.openConnection();
OutputStreamWriter wr =
new OutputStreamWriter(conn.getOutputStream());
BufferedReader br =
new BufferedReader(new InputStreamReader(blb.getBinaryStream()));

while ( (str = br.readLine())!=null)
data1 += str; //strBuffer.append(str); }
line = URLEncoder.encode("data", "UTF-8") + "=" +
URLEncoder.encode(data1, "UTF-8");


// Get the response
BufferedReader rd =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
wr.close() ;
catch (Exception e) {
e.printStackTrace(); // should do real exception handling
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42954
Suggest what? Joe already pointed out that you should not be using URLEncoder.encode.
Have you tried LearnNowOnline?
subject: string getting converted to ASCII