考慮我是一個使用HTML和Java的新手。如果您能夠查看我的問題並提供任何建議,我將不勝感激。我基本上嘗試按順序執行以下操作:HTML結果得到編碼
1)使用PostMethod類,通過POST
方法在Java中發送HTTP請求。
2)取結果。我接收的結果是HTML格式
3)的實際結果包含像", ;, :
等所有的報價是在結果轉換爲實體(& QUOT)(htmlOutput字符串)
我的問題是字符下列。如何避免獲取編碼結果。有沒有一種很好的方式來獲得結果作爲原始字符串,不包含實體(& quot)?以下是我使用的代碼。
int statusCode = HttpStatus.SC_OK;
String scriptOutput = "";
PostMethod runnerMethod = new PostMethod(url);
try {
runnerMethod.setRequestHeader("X-Forwarded-For", LOCAL_MACHINE_IP);
runnerMethod.addParameter("script", serializedScript);
statusCode = client.executeMethod(runnerMethod);
if (statusCode != HttpStatus.SC_OK) {
scriptOutput = "HTTP Post request failed with statusCode" + statusCode +
runnerMethod.getStatusText();
throw new Exception(scriptOutput);
}
String htmlOutput = runnerMethod.getResponseBodyAsString();
scriptOutput = StringUtils.substring(htmlOutput, StringUtils.indexOf(htmlOutput,"Script:") + 8, StringUtils.indexOf(htmlOutput, "<BR/>"));
return scriptOutput;
} catch (IllegalArgumentException e) {
String errMsg = String.format("Error during Background script execution on instance. opId = %s, instanceUrl = %s, HTTP Status Code = %d, Err Message = %s",
opId, instanceUrl, statusCode, e.getMessage());
return errMsg;
}
catch (Exception e)
{
String errMsg = String.format("Error during Background script execution on instance. opId = %s, instanceUrl = %s, HTTP Status Code = %d, Err Message = %s",
opId, instanceUrl, statusCode, e.getMessage());
return errMsg;
}
finally {
runnerMethod.releaseConnection();
}
輸出樣本如下:
你應該解釋一下你在「點」3)中的「轉換」是什麼意思你確定在'htmlOutput'變量中沒有HTML實體(" s等)? – pwes 2014-08-29 07:03:29
@pwes:我根據你的建議澄清了一點。爲了回答你的問題,htmlOutput確實包含HTML實體,在這種情況下,「而不是實際的」,讓我知道如果我的問題是清楚的 – animageofmine 2014-08-29 07:12:00