Encoding of text in HTTP multipart request

M

Matthias

Hi all,

I try to make a multipart HTTP request in Java to upload a wav file.
I have a problem with the encoding of additional textdata in this
request.
In my case it is a sessionID that is assigned randomly. If this ID
contains the character "%" the server receives "%25" instead (this is
the urlencoded representation of the character) and rejects the
request. Otherwise (in case of no % in the request) everything works
fine.

Here is the important sample of my code:

.....
URL url = new URL(urlstring);
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Connection", "Keep-Alive";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "--*****";

connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",multipart/form-data;
boundary=" + boundary);
connection.setRequestProperty("Accept-Charset","iso-8859-1,*,utf-8");
DataOutputStream dataoutputstream = new
DataOutputStream(connection.getOutputStream());

//wav file
dataoutputstream.writeBytes(twoHyphens + boundary + lineEnd);
dataoutputstream.writeBytes("Content-Disposition: form-data; name=\""
+ itenname
+ "\"; filename=\"" + filepath +"\"" + lineEnd);
dataoutputstream.writeBytes("Content-Type: audio/x-wav" +lineEnd +
lineEnd);
/* ...read file and send content via dataoutputstream.write(buffer, 0,
bufferSize) here everything works fine*/
dataoutputstream.writeBytes(lineEnd);

//text
dataoutputstream.writeBytes(twoHyphens + boundary + lineEnd);
dataoutputstream.writeBytes("Content-Disposition: form-data; name=\""
+ itemname + "\"" + lineEnd);
dataoutputstream.writeBytes("Content-Type: text/plain;
charset=iso-8859-1"+lineEnd);
dataoutputstream.writeBytes(lineEnd);
dataoutputstream.writeBytes(sessionID);
dataoutputstream.writeBytes(lineEnd);

//End of request
dataoutputstream.writeBytes(twoHyphens + boundary + twoHyphens +
lineEnd);
dataoutputstream.flush();
dataoutputstream.close();
.....


Example: when sessionID has the value "abcd%abcd" the server receives
"abcd%25abcd" and rejects the request

So the problem has to do with the encoding. How can I achieve, that
the server gets the text without the url enconding.

Thanks a lot

Matthias
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top