Announcement

Collapse
No announcement yet.

ISO-8859-1 Encoding-Problem beim HTTP-POST und XML

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ISO-8859-1 Encoding-Problem beim HTTP-POST und XML

    Hallo zusammen,

    ich muss ein XML-Dokument über HTTP-POST im Format ISO-8859 versenden.
    Leider kommt immer UTF-8 auf den Stream. Was mache ich hier falsch, bzw. wie kann ich sicherstellen, dass ich wirklich im ISO-Format sende?

    Vielen Dank im Voraus.

    Grüße,
    Marcus

    CODE:

    XMLHelper helper = new XMLHelper();
    Document domObject = helper.parseXmlFile("test.xml");

    // Aufbauen der Test-Verbindung
    URL url = new URL("http://www.httptest.de");
    URLConnection connection = url.openConnection();

    connection.setDoOutput(true);
    connection.setDoInput(true);

    if (connection instanceof HttpURLConnection)
    {
    ((HttpURLConnection)connection).setRequestMethod(" POST");
    }
    connection.setRequestProperty("Content-type", "application/xml; charset=ISO-8859-1");

    //Ausgehender Stream
    OutputStream out = connection.getOutputStream();
    out.write(helper.dom2Bytes(domObject, "ISO-8859-1"));
    out.close();

    //************************************************** ******************

    // Liest eine XML-Datei ein und erstellt daraus ein DOM-Dokument
    protected Document parseXmlFile(String fileName)
    {
    try
    {
    //Builder Factory erschaffen
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //Erschafft den Builder und parst durch das File
    Document doc = factory.newDocumentBuilder().parse(new File(fileName));
    return doc;
    }
    catch (Exception e)
    {
    return null;
    }
    }

    //************************************************** ***

    // Liest DOM-Document ein und wandelt es in ein ByteArrayStream um
    protected byte[] dom2Bytes(Document doc, String encoding) throws Exception
    {
    try
    {
    doc.normalize();
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    // Erschaffen eines Transformers
    Transformer xformer = TransformerFactory.newInstance().newTransformer();

    // Schreibt das DOM-document in einen String
    Source source = new DOMSource(doc);
    Result result = new StreamResult(outStream);
    xformer.transform(source, result);
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    return outStream.toByteArray();
    }
    catch (Exception e)
    {
    throw new Exception("Fehler beim Umwandeln eines DOM-Objekts in Bytes.");
    }
    }
Working...
X