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
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