title: String content sent as byte[] gets its content escaped as query string
body: <p>I need to send a String over the network, and I'm getting it on the other side in a REST Controller in Spring Boot.</p>
  
  <p>I'm sending the data with the following lines:</p>
  
  <pre><code>byte[] content = sw.toString().getBytes(StandardCharsets.UTF_8); //sw is StringWriter
  httpUrlConnection = (HttpURLConnection)new URL("http://127.0.0.1:8080/upload").openConnection();
  httpUrlConnection.setDoInput(false);
  httpUrlConnection.setDoOutput(true);
  httpUrlConnection.setRequestMethod("POST");
  httpUrlConnection.setFixedLengthStreamingMode(content.length);
  httpUrlConnection.connect();
  httpUrlConnection.getOutputStream().write(content);
  httpUrlConnection.getOutputStream().flush();
  </code></pre>
  
  <p>And I'm receiving like this:</p>
  
  <pre><code>@RequestMapping(value="/upload", method = RequestMethod.POST)
  public ResponseEntity&lt;String&gt; upload(@RequestBody byte[] uploadedData)
      throws NoSuchAlgorithmException, InvalidKeyException, IOException {
    if(uploadedData == null) {
      log.info("Uploaded data was null.");
      return new ResponseEntity&lt;String&gt;("Data was null after upload.", HttpStatus.INTERNAL_SERVER_ERROR);
    }
  
    String receivedData = new String(uploadedData, StandardCharsets.UTF_8);
    log.info("" + receivedData);
  </code></pre>
  
  <p>But while what I'm sending is this...</p>
  
  <pre><code>-----BEGIN CERTIFICATE REQUEST-----
  MIIB0DCCATkCAQAwgY8xCzAJBgNVBAYTAkFVMSgwJgYDVQQKDB9UaGUgTGVnaW9u
  IG9mIHRoZSBCb3VuY3kgQ2FzdGxlMRIwEAYDVQQHDAlNZWxib3VybmUxETAPBgNV
  BAgMCFZpY3RvcmlhMS8wLQYJKoZIhvcNAQkBFiBmZWVkYmFjay1jcnlwdG9AYm91
  bmN5Y2FzdGxlLm9yZzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAqE+pEyKS
  21JXgeldS83E+SSmQ4s2xYPqV7Yw2ebUKsxnbBz1KcXHdHS1MJ9PYzBJgogdigZW
  6hEQ83edP/Ay/EQzGqeKUzFqNEsQh3PSdbF9N5k7b81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%0A-----END+CERTIFICATE+REQUEST-----%0D%0A=
  </code></pre>
  
  <p>This is not good because it kills the PEM parser.</p>
  
  <p>However, I could overcome this problem by using the following transformations, and the String becomes parseable...</p>
  
  <pre><code>  receivedData = receivedData.replace("-----BEGIN+CERTIFICATE+REQUEST-----", "-----BEGIN CERTIFICATE REQUEST-----");
    receivedData = receivedData.replace("-----END+CERTIFICATE+REQUEST-----", "-----END CERTIFICATE REQUEST-----");
    receivedData = receivedData.replace("%0D%0A", "\r\n");
    receivedData = receivedData.replace("%2F", "/");
    receivedData = receivedData.substring(0, receivedData.length()-1);
  </code></pre>
  
  <p>In which case parsing was successful...</p>
  
  <pre><code>2014-10-18 16:26:43.864  INFO 2432 --- [nio-8080-exec-1] demo.HelloController                     : PemParser returned: org.spongycastle.pkcs.PKCS10CertificationRequest@f5c81fb9
  2014-10-18 16:26:43.870  INFO 2432 --- [nio-8080-exec-1] demo.HelloController                     : SUCCESS
  </code></pre>
  
  <p>...but I don't think this is a really viable way of doing things as it doesn't look very stable. There has to be a better way. I tried <code>URLEncoder.decode(receivedData, "UTF-8");</code> but it removes <code>+</code> characters from inside the actual data, which is also not good because it makes the data unparseable.</p>
  
  <p>Does anyone have any useful ideas on how to prevent this <code>URL escaping</code> when I'm sending a <code>String</code> over the network?</p>