首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向服务器发送XML时出现HTTP 500错误

向服务器发送XML时出现HTTP 500错误
EN

Stack Overflow用户
提问于 2011-07-12 20:40:53
回答 3查看 10.2K关注 0票数 0

我试图将一个XML文件发送到我的RESTful web服务器,并接收一个XML文件作为回报,但是,我得到了一个500个错误。

sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436) at java.io.IOException: java.io.IOException:服务器返回的HTTP响应代码: 500用于URL:http://sps-psa-240:8080/NMCJWS/rest/jmsmon2/pub at SendXML.send(SendXML.java:151) 在SendXML.main(SendXML.java:39)

第151行是InputStream response = uc.getInputStream();

如果取消注释System.out.println(((HttpURLConnection) uc).getResponseCode());,那么在OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream());上也会出现相同的错误

我知道服务器可以工作,因为同事在Obj中有这样的工作。

这是我的代码:

代码语言:javascript
运行
复制
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
        }

    }
}
EN

Stack Overflow用户

发布于 2011-07-12 22:17:36

确保使用正确的编码。URLEncoder.encode将内容安全地传输为“application/x form-urlencoded”,但您可能希望使用UTF-8。

此外,new OutputStreamWriter(...)应该指定所需的编码。您目前正在使用标准平台编码,可能是iso-8859-1。

第三,不要自找麻烦地对待URLConnection,如果你身边有很多小人,这会让你的生活更轻松。

这是在Resty中完成的发送方法(免责声明:我是它的作者)。与其他客户端库一样,HTTPClient也是需要考虑的另一个选择。

代码语言:javascript
运行
复制
import us.monoid.web.Resty;
import static us.monoid.web.Resty.*;

Resty r = new Resty();
String result = r.text(urladdress, new Content("text/xml", file.getBytes("UTF-8"))).toString();
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6670860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档