title: how to send byte array in json post request?
body: <p>I have a  wcf service which accepts   <code>byte[] serialData</code>, now am developing a java client which needs to consume the same method.</p>
  
  <p>When i sent bytearray to the service as a json post request , it is getting an exception as <code>java.io.IOException: Server returned HTTP response code: 400</code> </p>
  
  <p>Here is my code:</p>
  
  <p><strong>wcf method:</strong></p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "saveSerialNumbers", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
  Dictionary&lt;string, object&gt; saveSerialNumbers(byte[] serialData);
  </code></pre>
  
  <p><strong>Java Client:</strong></p>
  
  <pre><code>for (int i = 1; i &lt; 100; i++) {      
                  sb.append(String.valueOf(gen()));
      }
  byte[]   bytesEncoded = Base64.encodeBase64(sb.toString().getBytes());
  String json = "{\"serialDataByte\":\""+sb.toString()+"\"}";
  </code></pre>
  
  <p>This is my postrequest method:</p>
  
  <pre><code>public String getResultPOST(String jsonObject,String uri,String method) throws Exception{
          try {
              URL url = new URL(uri+method);
              System.out.println(url.toString());
              URLConnection connection = url.openConnection();
              connection.setDoOutput(true);
              connection.setRequestProperty("Content-Type", "application/json");
              connection.setConnectTimeout(5000);
              connection.setReadTimeout(5000);
              OutputStreamWriter out;
              try {
                  out = new OutputStreamWriter(connection.getOutputStream());
                    out.write(jsonObject);
                      out.close();
              } catch (Exception e) {
                  /
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
  
              String line = "";
              StringBuilder builder = new StringBuilder();
              BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              while ((line = in.readLine()) != null) {
                  builder.append(line);
              }
  
              in.close();
              return builder.toString();
          } catch (Exception e) {
              throw e;
              //here is the exception
          }
      }
  </code></pre>
  
  <p>Here is my method call:</p>
  
  <pre><code> String json = "{\"serialData\":\""+ new String(bytesEncoded)  +"\",\"guProductID\":\""+guProductID+"\",\"guStoreID\":\""+guStoreID+"\",\"securityToken\":\""+SecurityToken+"\"}";
   String serialContract  = serialClient.getResultPOST(json, "http://localhost:3361/EcoService.svc/Json/", "saveSerialNumbers");
  </code></pre>