Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:01:18+00:00 2026-05-23T22:01:18+00:00

I am trying to send an XML file to my RESTful web server, and

  • 0

I am trying to send an XML file to my RESTful web server, and receive a XML file in return, however, I am getting a 500 error.

java.io.IOException: Server returned HTTP response code: 500 for URL:
http://sps-psa-240:8080/NMCJWS/rest/jmsmon2/pub at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at SendXML.send(SendXML.java:151)
at SendXML.main(SendXML.java:39)

Line 151 is InputStream response = uc.getInputStream();

If I uncomment System.out.println(((HttpURLConnection) uc).getResponseCode());,
then I get the same error on OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream());

I know the server works because a coworker has this working in Obj-C.

Here is my code:

public class SendXML 
{
    public static void main(String[] args) throws SAXException, XPathExpressionException, ParserConfigurationException, 
                                                IOException, TransformerException 
    {   
        String xml = generateXML("AC24", "/fa/gdscc/dss24-apc");
        send("localhost", xml); 
    }

    public static String generateXML(String conn, String funcAddr) throws ParserConfigurationException, SAXException, 
                                                IOException, XPathExpressionException, TransformerException 
    {
        /*
         * <?xml version="1.0" encoding="UTF-8"?>
            <JMSMON2Req>
                <SubItem UID="iPAD-2031e616-de74-44a7-9292-3745d2b1ba21">
                    <FuncAddr>/fa/gdscc/con1-ac25</FuncAddr>
                    <ItemName>AZANG</ItemName>
                    <ItemName>ELANG</ItemName>
                    <Metadata key="UID">iPAD-2031e616-de74-44a7-9292-3745d2b1ba21</Metadata>
                    <Metadata key="CONN">1</Metadata>
                </SubItem>
            </JMSMON2Req>
         */

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse("http://sps-psa-240:8080/NMCWS/rest/conn/subsys/prof?ss=" + conn + "&pt=IPAD_DASHBOARD");

        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile("/SubscrProf/DataItem/DataItemName");

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        //build xml
        Document output = builder.newDocument();

        //create root
        org.w3c.dom.Element root = output.createElement("JMSMON2Req");
        output.appendChild(root);

            //create subitem
            org.w3c.dom.Element subItemNode = output.createElement("SubItem");
            subItemNode.setAttribute("UID", "IPAD-CN1-DSS26-SC151-PN230-AC26");
            root.appendChild(subItemNode);

                //create funcAddr
                org.w3c.dom.Element funcAddrNode = output.createElement("FuncAddr");
                Text text = output.createTextNode(funcAddr);
                funcAddrNode.appendChild(text);
                subItemNode.appendChild(funcAddrNode);

                //create itemname
                for (int i = 0; i < nodes.getLength(); i++) 
                {
                    org.w3c.dom.Element itemNameNode = output.createElement("SubItem");
                    text = output.createTextNode(nodes.item(i).getTextContent());
                    itemNameNode.appendChild(text);
                    subItemNode.appendChild(itemNameNode);
                }

                //create metadata uid
                org.w3c.dom.Element metaDataNode = output.createElement("Metadata");
                metaDataNode.setAttribute("key", "UID");
                text = output.createTextNode("IPAD-CN1-DSS26-SC151-PN230-AC26");
                metaDataNode.appendChild(text);
                subItemNode.appendChild(metaDataNode);

                //create metadata conn
                org.w3c.dom.Element metaDataNode2 = output.createElement("Metadata");
                metaDataNode2.setAttribute("key", "CONN");
                text = output.createTextNode("4");
                metaDataNode2.appendChild(text);
                subItemNode.appendChild(metaDataNode2);

        /////////////////
        //Output the XML

        //set up a transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        //create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult out = new StreamResult(sw);
        DOMSource source = new DOMSource(output);

        trans.transform(source, out);
        String xmlString = sw.toString();

        //print xml
        System.out.println("Here's the xml:\n" + xmlString);

        return xmlString;
    }

    public static void send(String urladdress, String file) throws MalformedURLException, IOException
    {
        String charset = "UTF-8";
        String s = URLEncoder.encode(file, charset);

        // Open the connection and prepare to POST
        URLConnection uc = new URL(urladdress).openConnection();
        uc.setDoOutput(true);
        uc.setRequestProperty("Accept-Charset", charset);
        uc.setRequestProperty("Content-Type","text/xml");

        try
        {
            //System.out.println(((HttpURLConnection) uc).getResponseCode());
            OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream());
            out.write(s);
            out.flush();

            InputStream response = uc.getInputStream();
            BufferedReader r = new BufferedReader(new InputStreamReader(response));
            String line;

            while ((line = r.readLine()) != null)
                System.out.println(line);


            out.close();
            response.close();
        }
        catch (IOException e)
        { 
            e.printStackTrace();    // should do real exception handling
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:01:18+00:00Added an answer on May 23, 2026 at 10:01 pm

    I figured out my problem. I had to encode xmlString in UTF-8

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send an XML file to a server as part of the
I am trying to write a servlet that will send a XML file (xml
EDIT : I am trying to send an xml file as a post request
I am trying to send a simple XML file of the format given in
I am trying to send an XML file that I created in my memorystream.
I am trying to send a SOAP request from a xml file and send
i am trying to read the xml file but somehow i am getting this
I trying to import an XML file, and send bits and pieces to a
I'm trying to send a file from an applet to my server GWT. In
I am trying to send an XML file from a textfield in my html,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.