我的连接到url(xml)文件时遇到问题,并创建了java对象。到目前为止,我无法连接到我的链接,因为"服务器返回了HTTP响应代码:415为URL"
我的链接:http://services.odata.org/V3/Northwind/Northwind.svc/Employees
我的代码(不完整,但抛出异常):
package logic;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLReader {
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
//JsonObject requestJson = new JsonObject();
String url = "http://services.odata.org/V3/Northwind/Northwind.svc/Employees";
URL myurl = new URL(url);
HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/xml; charset=utf-8");
con.setRequestProperty("Accept", "application/xml");
con.setRequestProperty("Method", "GET");
InputStream xml = con.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
doc.getDocumentElement().normalize();
}
答案 0 :(得分:0)
请 con.setRequestProperty(" Accept"," application / xml"); 至 con.setRequestProperty("接受"," * / xml");
答案 1 :(得分:0)
在我的情况下,我将“application/xml”更改为“text/xml”并且它起作用了。