2011-07-12 133 views
0

我試圖將XML文件發送到我的REST風格的Web服務器,並接收XML文件作爲回報,但是,我收到了500錯誤。將XML發送到服務器時發生HTTP 500錯誤

java.io.IOException的:服務器返回的HTTP響應代碼:500爲URL: http://sps-psa-240:8080/NMCJWS/rest/jmsmon2/pub在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436) 在SendXML。發送(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-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 
     } 

    } 
} 
+0

HTTP 500通常意味着服務器代碼會拋出一個異常(通常是由服務器代碼中的錯誤引起的,而該錯誤又可能由不良的用戶輸入觸發)。閱讀服務器日誌。關於HTTP 500錯誤的原因是什麼? *那就是你的問題的答案。 – BalusC

+0

我無法測試,但在讀取輸入之前,您可能需要關閉輸出流。 – Mufaka

+0

在讀取輸入之前關閉輸出流並沒有修復它。 – Raptrex

回答

1

我想通了我的問題。我必須以UTF-8格式編碼xmlString

0

看看服務器上的日誌。什麼導致500錯誤?

這是一個REST風格的Web服務,還是您正在提交的SOAP Web服務?

考慮使用某種類型的XML < - >對象框架,如JAXBXStream

考慮使用某種類型的REST風格的Web服務框架,如JerseyRestEasy

考慮使用某種SOAP框架像JAX-WSApache Axis

+0

其寧靜的網絡服務 – Raptrex

+0

我們仍然想知道服務器日誌顯示的內容。另外,如果您使用的是休息框架,可能會啓用一些額外的調試功能,例如[LoggingFilter](http://jersey.java.net/nonav/apidocs/1.8/jersey/com/sun/) jersey/api/client/filter/LoggingFilter.html)。 – Jim

0

確保您使用的是正確的編碼。 URLEncoder.encode正在將內容安全地轉換爲'application/x-www-form-urlencoded',但您可能想要使用UTF-8。

此外,new OutputStreamWriter(...)應指定所需的編碼。您目前正在使用可能是iso-8859-1的標準平臺編碼。第三,如果你周圍有大量的庫,那麼你不要試圖自己搞砸URLConnection,這會讓你的生活更輕鬆。

這是在Resty中完成的發送方法(免責聲明:我是它的作者)。與其他客戶端庫一樣,HTTPClient是另一個選擇。

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

我認爲我的發送方法正確使用UTF-8 encodoing – Raptrex

+0

不,它不。您可能只是很幸運,因爲ISO-8859-1是UTF-8的有效子集,您首先使用RFC1738對其進行編碼。儘管內容可能完好無損,但服務器無法確定您使用RFC1738的內容。 –

+0

那麼,我會改變這個? 'String charset =「UTF-8」; \t \t String s = URLEncoder.encode(file,charset);'? – Raptrex

相關問題