Java“序言中不允许的内容。”
Posted
技术标签:
【中文标题】Java“序言中不允许的内容。”【英文标题】:Java "Content not allowed in prolog.” 【发布时间】:2020-07-03 13:37:49 【问题描述】:我正在编写一个客户端,它可以保留槽位、查看可用槽位、查看您已预订的槽位和取消保留槽位。我的代码适用于除保留插槽之外的所有内容。
以下是预留位置的代码。
while(hotelBooked == false && bandBooked == false)
// This works
xmlString = XMLRequest.availability(requestID, USERNAME, PASSWORD);
ArrayList<String> availSlots = checkAvailiabilityOrBookings(xmlString);
for(int i = 0; i < availSlots.size(); i++)
TimeUnit.SECONDS.sleep(1);
System.out.println("availSlots.get(" + i + "): " + Integer.parseInt(availSlots.get(i).trim()));
// generate a unique ID based off time
requestID = genRequestID();
System.out.println("REQUESTID" + requestID);
//Something goes wrong around here
xmlString = XMLRequest.Reservation(requestID, USERNAME, PASSWORD, 134);
// breaks in this method
hotelBooked = reserveSlot(xmlString, hotelNum);
if(hotelBooked == true)
bandBooked = reserveSlot(xmlString, bandNum);
if(bandBooked == false)
requestID = genRequestID();
System.out.println("REQUESTID " + requestID);
xmlString = XMLRequest.cancel(requestID, USERNAME, PASSWORD, Integer.parseInt(availSlots.get(i).trim()));
cancelSlot(xmlString, hotelNum);
// if
else
requestID = genRequestID();
System.out.println("REQUESTID" + requestID);
xmlString = XMLRequest.bookings(requestID, USERNAME, PASSWORD);
bookedSlots = checkAvailiabilityOrBookings(xmlString);
System.out.println("1st time - Booked slots:");
System.out.println(bookedSlots.toString());
break;
// if
下面是破解的方法
// reserve a slot
public static Boolean reserveSlot(String xmlString, String hotelOrBand)
System.out.println("Entered reserveSlot");
Response recMsgOutput;
PutMethod putMethod;
boolean booked = false;
try
if(hotelOrBand.equals(String.valueOf(3010)))
putMethod = putMethodHotel;
else
putMethod = putMethodBand;
/*
* Set the request's entity (body).
*/
System.out.println("Set the request's entity (body)");
RequestEntity entity = new StringRequestEntity(xmlString);
putMethod.setRequestEntity(entity);
/*
* Set the put method's headers
*/
System.out.println("Set the put method's headers");
putMethod.addRequestHeader("Content-Type", "application/xml");
putMethod.addRequestHeader("Accept", "application/xml");
/*
* Create a client and the execute the put method.
*/
System.out.println("Create a client and the execute the put method.");
HttpClient client = new HttpClient();
int responseCode = client.executeMethod(putMethod);
while(responseCode != HttpStatus.SC_OK)
client = new HttpClient();
responseCode = client.executeMethod(putMethod);
TimeUnit.SECONDS.sleep(1);
// while
if (responseCode == HttpStatus.SC_OK)
System.out.println("Message uri: " + Response.getMsgURI(putMethod.getResponseBodyAsString()));
String [] message = Response.getMsgURI(putMethod.getResponseBodyAsString()).split("/");
String msgNum = message[message.length - 1];
String recMsgArg = "http://jewel.cs.man.ac.uk:" + hotelOrBand + "/queue/msg/" + msgNum + "?username=0ih058&password=4UhMf9";
System.out.println("recMsgArg " + recMsgArg);
String [] recMsgArgArray = new String[1];
// Send requests to ClientRecMsg
recMsgArgArray[0] = recMsgArg;
System.out.println("recMsgArgArray " + recMsgArgArray[0]);
recMsgOutput = ClientRecMsg.main(recMsgArgArray);
Matcher matcher1 = Pattern.compile("\\d+").matcher(recMsgOutput.toString());
matcher1.find();
int responseNum = Integer.valueOf(matcher1.group());
System.out.println("num: " + responseNum);
if(responseNum == 200)
booked = true;
else if(responseCode != HttpStatus.SC_OK)
System.out.println("Error code:" + responseCode);
System.out.println("Error message:" + putMethod.getResponseBodyAsString());
//try
输出这个
availSlots.get(4): 135
REQUESTID 1584934385
Entered reserveSlot
Set the request's entity (body)
Set the put method's headers
Create a client and the execute the put method.
[Fatal Error] :1:1: Content is not allowed in prolog.
uk.ac.manchester.cs.comp28112.lab2.ParseException
at uk.ac.manchester.cs.comp28112.lab2.Response.getMsgURI(Response.java:179)
at uk.ac.manchester.cs.comp28112.lab2.ClientReserve.reserveSlot(ClientReserve.java:527)
at uk.ac.manchester.cs.comp28112.lab2.ClientReserve.reserveRequest(ClientReserve.java:164)
at uk.ac.manchester.cs.comp28112.lab2.ClientReserve.main(ClientReserve.java:77)
用于预订的 XML 代码如下
static public String Reservation(String request_id, String username,
String password, int slot_id) throws RequestException
try
XMLRequest.createBuilder();
Document document = documentBuilder.newDocument();
Element reserve_element = document.createElement(RESERVE_ELEMENT);
document.appendChild(reserve_element);
Node id_element = document.createElement(REQUEST_ID_ELEMENT);
id_element.appendChild(document.createTextNode(request_id));
reserve_element.appendChild(id_element);
Node username_element = document.createElement(USERNAME_ELEMENT);
username_element.appendChild(document.createTextNode(username));
reserve_element.appendChild(username_element);
Node password_element = document.createElement(PASSWORD_ELEMENT);
password_element.appendChild(document.createTextNode(password));
reserve_element.appendChild(password_element);
Node slot_id_element = document.createElement(SLOT_ID_ELEMENT);
slot_id_element.appendChild(document.createTextNode(new Integer(
slot_id).toString()));
reserve_element.appendChild(slot_id_element);
return XMLRequest.toString(document);
catch (ParserConfigurationException e)
throw new RequestException(e);
catch (TransformerConfigurationException e)
throw new RequestException(e);
catch (TransformerFactoryConfigurationError e)
throw new RequestException(e.getException());
catch (TransformerException e)
throw new RequestException(e);
下面是Response.getMsgURI()的方法
static public String getMsgURI(String xmlString) throws ParseException
try
Response.createBuilder();
InputSource source = new InputSource(new StringReader(xmlString));
Node node = (Node) msgIdXPathExpression.evaluate(source, XPathConstants.NODE);
return node.getTextContent();
catch (XPathExpressionException e)
throw new ParseException();
catch (ParserConfigurationException e)
throw new ParseException();
下面是 putMethod.getResponseBodyAsString() 的输出
Status: 500 Internal Server Error
Content-Type: text/html
<html><body><h1>500 Internal Server Error</h1></body></html>
我认为这与在同一方法中发出多个 xml 请求有关,因为当我首先发出预订请求时它运行良好,但是当我尝试在之后立即发出另一个 xml 请求时也会卡住。
很抱歉包含这么多代码,非常感谢您的帮助。
【问题讨论】:
“序言”是 XML 文档的<?
部分。您的 XML 似乎格式不正确;这就是你需要发布的内容。
由于错误发生在Response.getMsgURI()
方法中,除了putMethod.getResponseBodyAsString()
调用返回的字符串之外,您是否认为向我们展示该方法也可能相关?但是话又说回来,一旦您看到返回的字符串,您可能会明白为什么它不解析为 XML,并且方法代码可能无关紧要。但是,我们怎么知道,因为我们也看不到。
抱歉,我是新手,我觉得我要发布大量代码,我不想发布不必要的内容。
主要要理解的是,“prolog 中不允许的内容”通常意味着您向 XML 解析器提供了一些不以“
【参考方案1】:
问题是我试图重用 putMethod 对象,每次发出请求时都需要创建一个新对象。我不知道为什么会这样。
【讨论】:
以上是关于Java“序言中不允许的内容。”的主要内容,如果未能解决你的问题,请参考以下文章