Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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 HTML结果被编码_Java_Html - Fatal编程技术网

Java HTML结果被编码

Java HTML结果被编码,java,html,Java,Html,假设我是一个使用HTML和Java的新手。如果您能了解我的问题并提出任何建议,我将不胜感激。我基本上是尝试按顺序执行以下操作: 1) 使用PostMethod类在Java中通过POST方法发送HTTP请求 2) 获取结果。我收到的结果是HTML格式的 3) 实际结果包含诸如“,;,:等字符。所有引号都转换为结果(htmlOutput字符串)中的实体(") 我的问题如下。如何避免获取编码结果。是否有一种好的方法可以将结果作为不包含实体(“实体”)的原始字符串获取?以下是我使用的代码

假设我是一个使用HTML和Java的新手。如果您能了解我的问题并提出任何建议,我将不胜感激。我基本上是尝试按顺序执行以下操作:

1) 使用PostMethod类在Java中通过
POST
方法发送HTTP请求

2) 获取结果。我收到的结果是HTML格式的

3) 实际结果包含诸如
“,;,:
等字符。所有引号都转换为结果(htmlOutput字符串)中的实体(")

我的问题如下。如何避免获取编码结果。是否有一种好的方法可以将结果作为不包含实体(“实体”)的原始字符串获取?以下是我使用的代码

        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();
        }
int statusCode=HttpStatus.SC\u正常;
字符串scriptOutput=“”;
PostMethod runnerMethod=新的PostMethod(url);
试一试{
setRequestHeader(“X-Forwarded-For”,本地机器IP);
addParameter(“脚本”,serializedScript);
statusCode=client.executeMethod(runnerMethod);
if(statusCode!=HttpStatus.SC\u OK){
scriptOutput=“HTTP Post请求失败,状态代码为”+statusCode+
runnerMethod.getStatusText();
抛出新异常(scriptOutput);
}
字符串htmlOutput=runnerMethod.getResponseBodyAsString();
scriptOutput=StringUtils.substring(htmlOutput,StringUtils.indexOf(htmlOutput,“脚本:”)+8,StringUtils.indexOf(htmlOutput,“
”); 返回脚本输出; }捕获(IllegalArgumentException e){ String errMsg=String.format(“在实例上执行后台脚本时出错。opId=%s,instanceUrl=%s,HTTP状态代码=%d,错误消息=%s”, opId,instanceUrl,statusCode,e.getMessage(); 返回errMsg; } 捕获(例外e) { String errMsg=String.format(“在实例上执行后台脚本时出错。opId=%s,instanceUrl=%s,HTTP状态代码=%d,错误消息=%s”, opId,instanceUrl,statusCode,e.getMessage(); 返回errMsg; } 最后{ runnerMethod.releaseConnection(); }
输出示例如下所示:


您所做的是发布到HTTP服务器。我假定您使用的是Apache Commons HTTPClient。getResponseBodyAsString()方法中没有任何内容会将引号转义到HTML实体

可能您试图在服务器端发送一个双JSON编码的对象(因此它首先被编码为通常的表示形式,然后被编码为JSON字符串,这将解释实体)


正确的解决方案是消除双重编码。如果您不控制服务器端,您可以使用
.replaceAll(“”,“\”)
或使用Apache Commons StringEscapeUtils。

您应该解释第3点中“转换”是什么意思)您确定没有HTML实体(“s等”)吗在
htmlOutput
variable?@pwes:根据您的建议,我做了进一步澄清。为了回答您的问题,htmlOutput确实包含HTML实体,在本例中是“而不是实际的”“。如果我的问题清楚,请告诉我。是,结果服务器返回的是JSON字符串。我正在使用ApacheCommonsHttpClient。从服务器返回结果时,如何消除双重编码?顺便说一句,我确实想过使用replaceall,但那只解决了一个问题。我不确定我还应该替换哪些字符。我正在开发一个通用客户端库,因此结果可能包含其他字符,这些字符将被更改为HTML实体。建议?正如我在上面所写的,StringEscapeUtils(它是ApacheCommonsLang的一部分)可以取消对所有HTML实体的扫描。我不知道在服务器上做了什么,因此如果没有更多信息,我无法建议如何消除双重编码。谢谢。在服务器端,将执行一个脚本(javascript),并使用以下JSON编码器将结果字符串编码为JSON:。示例:[{“key1”:{“subkey1”:“value1”},{“key2”:{“subkey2”:“value2”}]但这显然不是通过电线发送的,否则您的问题甚至不会出现。您发送JSON的文档类型是什么?可能有一个服务器组件因为发送内容类型text/HTML而转义HTML实体?