0

I'm trying to write a byte buffer which contains this kind of strings &field_Value=

kggsW9YizUuRpjB/RtppZImnC8SekMGAWGnCmw+wPMwn7fWJva0QbSK/Slt/DDsZ

through a HttpURLConnection, but I receive &field_Value=

kggsW9YizUuRpjB/RtppZImnC8SekMGAWGnCmw wPMwn7fWJva0QbSK/Slt/DDsZ

So '+' is the only character which is not send. It happens with all my strings that contain a '+' inside.

Could you tell me how to encode the strings or what I'm doing wrong.

This is my code:

Sender:

url = new URL(url_globalApp);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

        connection.setRequestProperty("Content-Language", "en-US");  
        connection.setRequestProperty("Accept-Charset", "UTF-8"); 
        connection.setUseCaches (false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream (
                connection.getOutputStream ());
        //**************** first try ************//
        wr.write(pairs_1.getBytes("UTF-8"), 0, pairs_1.length());

        //**************** second try ************//
            //wr.writeBytes(new String(pairs_1.getBytes(),"UTF-8"));

        //**************** third try ************//
            //wr.writeUTF(pairs_1);
            wr.flush ();
            wr.close ();

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    Map <String,String[]> parameters = request.getParameterMap();
    System.out.println(parameters.get("field_value_1")[0]);
Alex
  • 19,061
  • 10
  • 50
  • 66

1 Answers1

0

The value of a query parameter must be URL encoded.

The URL encoding of

kggsW9YizUuRpjB/RtppZImnC8SekMGAWGnCmw+wPMwn7fWJva0QbSK/Slt/DDsZ

is

kggsW9YizUuRpjB%2FRtppZImnC8SekMGAWGnCmw%2BwPMwn7fWJva0QbSK%2FSlt%2FDDsZ

Notice how / is encoded to %2F, and + is encoded to %2B.

You do it by calling

java.net.URLEncoder.encode("kggsW9YizUuRpjB/RtppZImnC8SekMGAWGnCmw+wPMwn7fWJva0QbSK/Slt/DDsZ", "UTF-8")
Andreas
  • 138,167
  • 8
  • 112
  • 195
  • Thanks, that's the right way to encode but when I try to get the parameter Map it doesn't work. At least the string it's ok on the server side. Do you have a solution to get the parameter Map? – Sergio Baquero Mar 11 '16 at 00:06
  • @SergioBaquero What does "doesn't work" mean? Is the map empty? What is value of `request.getQueryString()`? – Andreas Mar 11 '16 at 00:09
  • Yes, when I run `parameters.containsKey("field_"+String.valueOf(i))` it returns false, just when the request is encoded. – Sergio Baquero Mar 11 '16 at 00:11
  • @SergioBaquero Can't help if you don't answer my actual questions. – Andreas Mar 11 '16 at 00:14
  • Sorry, I didn't see, give me a second please – Sergio Baquero Mar 11 '16 at 00:15
  • Sorry @Andreas it took me some time to say the answer is `null` – Sergio Baquero Mar 11 '16 at 00:23
  • @SergioBaquero There is your answer. If `getQueryString()` is null, then no query parameters were given, so of course `getParameterMap()` is empty. – Andreas Mar 11 '16 at 00:26
  • But it just happens when it's encoded. Look at this please. some words are in spanish: `null valor started valor 2 valor Y3PT8kLdNLbncqH10v5iS6YK3LTkAbX51xfCw4b5hFNrxUUQp/5AkURZ3a2thFFM valor 5 valor 2016-03-10 valor 1` this is the output which measn `getQueryString()` is empty but info is there. – Sergio Baquero Mar 11 '16 at 00:31
  • I have no idea what I'm looking at. I don't see any `&field_Value=kggsW9YizUuRpjB%2FRtppZImnC8SekMGAWGnCmw%2BwPMwn7fWJva0QbSK%2FSlt%2FDDsZ` – Andreas Mar 11 '16 at 00:32
  • You're right. The `getQueryString()` return null both cases when info is encoded or not. But the parameter Map is empty just when info is encoded. – Sergio Baquero Mar 11 '16 at 00:36
  • What is value of `pairs_1`? What is value of `getParameterMap()`? with and without encoding, for both. Edit question, show MCVE. I'm off, night. – Andreas Mar 11 '16 at 00:41
  • Thanks @Andreas I was using `URLEncoder.encode()` with the entire query. Now I see my question was written wrong. – Sergio Baquero Mar 11 '16 at 14:18