title: How to fire HTTP post with content type XML along with query parameters?
body: <p>I am trying to fire a POST request. The data I am sending is XML format (I am doing this via JAXB), but I have to send along a request parameter. My problem is regarding the <strong>Content-Type</strong> (I've added some comments in the code). Which one should I use ?</p>
  
  <p>See below my code : </p>
  
  <pre><code>       private V callAndGetResponse(K request, Class&lt;K&gt; requestClassType, Class&lt;V&gt; responseClassType) throws Exception {
      JAXBContext jaxbContext = JAXBContext.newInstance(requestClassType, responseClassType);
  
      Marshaller marshaller = jaxbContext.createMarshaller();
      // set properties on marshaller
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
      marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", String.format(XML_HEADER_FORMAT, dtd));
      marshaller.marshal(request, System.out);
  
      URL wsUrl = new URL(primaryEndpointUrl);
      HttpURLConnection connection = openAndPrepareConnection(wsUrl);
      tryToMarshallWsRequestToOutputStream(request, marshaller, connection);
  
      printDebug(connection);
  
      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
      Object response = unmarshaller.unmarshal(connection.getInputStream());
  
      // cleanup
      connection.disconnect();
      return responseClassType.cast(response);
  }
  
  private HttpURLConnection openAndPrepareConnection(URL wsUrl) throws IOException {
      HttpURLConnection connection = (HttpURLConnection) wsUrl.openConnection();
      connection.setDoOutput(true);
      connection.setRequestProperty("Accept", "application/xml");
      connection.setRequestProperty("Accept-Charset", charset);
      // Both types ? Doesn't work
      connection.setRequestProperty("Content-Type", "application/xml;application/x-www-form-urlencoded");
      // Only app/xml ? it seems that query param is not added to request
      connection.setRequestProperty("Content-Type", "application/xml");
      // Only app/query param ? it seems that xml is not added to request
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  
      return connection;
  }
  
  private void tryToMarshallWsRequestToOutputStream(K request, Marshaller jaxbForRequestMarshaller,
          HttpURLConnection connection) throws JAXBException, IOException {
      OutputStream outputStream = null;
      try {
          outputStream = connection.getOutputStream();
          jaxbForRequestMarshaller.marshal(request, outputStream);
          addQueryParameters(outputStream);
      }
      finally {
          tryToClose(outputStream);
      }
  }
  
  private void addQueryParameters(OutputStream outputStream) throws IOException {
      String value = "none";
      String query = String.format("xmlmsg=%s", URLEncoder.encode(value, charset));
      outputStream.write(query.getBytes(charset));
  }
  
  protected void tryToClose(OutputStream outputStream) throws IOException {
      if (outputStream != null) {
          outputStream.close();
      }
  }
  </code></pre>
  
  <p>Thanks a lot!</p>