Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向服务器发送XML时出现HTTP 500错误_Java_Xml_Http - Fatal编程技术网

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

Java 向服务器发送XML时出现HTTP 500错误,java,xml,http,Java,Xml,Http,我试图向我的RESTful web服务器发送一个XML文件,并收到一个XML文件作为回报,但是,我得到了一个500错误 java.io.IOException:服务器返回了URL: 在 net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436) 在SendXML.send(SendXML.java:151)上 位于SendXML.main(SendXML.java:39) 第151行是In

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

java.io.IOException:服务器返回了URL: 在 net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436) 在SendXML.send(SendXML.java:151)上
位于SendXML.main(SendXML.java:39)

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

如果我取消注释
System.out.println(((HttpURLConnection)uc.getResponseCode()),,
然后我在
OutputStreamWriter out=newOutputStreamWriter(uc.getOutputStream())上得到了相同的错误

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

这是我的密码:

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
        }

    }
}
公共类SendXML
{
公共静态void main(字符串[]args)引发SAXException、XPathExpressionException、ParserConfiguration异常、,
IOException,TransformerException
{   
字符串xml=generateXML(“AC24”,“/fa/gdscc/dss24 apc”);
发送(“localhost”,xml);
}
公共静态字符串generateXML(String conn,String funcAddr)抛出ParserConfiguration异常、SAXException、,
IOException、XPathExpressionException、TransformerException
{
/*
* 
/fa/gdscc/con1-ac25
阿桑
伊朗
iPAD-2031e616-de74-44a7-9292-3745d2b1ba21
1.
*/
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);//永远不要忘记这一点!
DocumentBuilder=domFactory.newDocumentBuilder();
Document doc=builder.parse(“http://sps-psa-240:8080/NMCWS/rest/conn/subsys/prof?ss=“+conn+”&pt=IPAD_仪表板”);
XPath=XPathFactory.newInstance().newXPath();
XPathExpression expr=xpath.compile(“/SubscrProf/DataItem/DataItemName”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
//构建xml
文档输出=builder.newDocument();
//创建根
org.w3c.dom.Element root=output.createElement(“JMSMON2Req”);
output.appendChild(根);
//创建子项
org.w3c.dom.Element subItemNode=output.createElement(“SubItem”);
subItemNode.setAttribute(“UID”、“IPAD-CN1-DSS26-SC151-PN230-AC26”);
appendChild(子项节点);
//创建funcAddr
org.w3c.dom.Element funcAddrNode=output.createElement(“FuncAddr”);
Text Text=output.createTextNode(funcAddr);
funcAddrNode.appendChild(文本);
subItemNode.appendChild(funcAddrNode);
//创建项目名
对于(int i=0;i
查看s上的日志
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();