使用HttpURLConnection 连接webService时,得到的内容中“<”变成了“<”中文变成误 如题,怎么解决成正常的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这不是encoding问题,都是默认的为utf-8 例如我在程序中返回这些信息:<![CDATA[<Response><UserInfoSyncToISMPResp><StreamingNo></StreamingNo><ResultCode>-113</ResultCode><ResultDesc>参数错误</ResultDesc></UserInfoSyncToISMPResp></Response>]]>那HttpURLConnection用得到的soap就为这样:<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><UserInfoSyncToISMPResp xmlns="http://www.mbossvsop.com.cn/vsop"><response xmlns=""><![CDATA[<Response><UserInfoSyncToISMPResp><StreamingNo></StreamingNo><ResultCode>-113</ResultCode><ResultDesc>参数错误</ResultDesc></UserInfoSyncToISMPResp></Response>]]></response></UserInfoSyncToISMPResp></soapenv:Body></soapenv:Envelope>怎么出现这种情况 我的回答有误,不好意思,看看soap请求时有没有参数设置不转义,如果没的话只能自己解析了。 他们请求的我这边都可以正确获得,包括中文。但是就是我这边返回的信息,他们用HttpURLConnection获得时出现这种情况,但是我自己这边用wsdl文件生成的客户端调用是没有什么问题的 发下HttpURLConnection请求的代码看看。 用HttpURLConnection获得是木有问题的,再说对方平台调用,我们也不能修改。需要也可以发布出来HttpURLConnection connection = null; String soapAction = "userInfoSynSV"; String requestXML = getXML(); String serviceURL = "--------"; connection = openConnection(serviceURL, requestXML, soapAction);InputStream input = connection.getInputStream(); StringBuffer buf = new StringBuffer(); byte[] b = new byte[1024]; int length = -1; while ((length = input.read(b)) != -1) { buf.append(new String(b, 0, length, "UTF-8")); } return buf.toString();private static HttpURLConnection openConnection(String serviceURL, String requestXML, String soapAction) throws Exception { URL url = new URL(serviceURL); URLConnection connection = url.openConnection(); String host =url.getHost(); HttpURLConnection httpConn = (HttpURLConnection) connection; // Open the httpConn.setRequestProperty("Host", host); httpConn.setRequestProperty("Content-Length", requestXML.length() + ""); httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");// httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); httpConn.setRequestProperty("SOAPAction", soapAction); httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); // Everything's set up; send the XML that was OutputStream os = httpConn.getOutputStream(); os.write(requestXML.getBytes("utf-8")); os.flush(); os.close(); return httpConn; } 别把信息放在<![CDATA[]]标签里! 觉得12楼的说法不对,含有特殊字符的数据本来就该放在CDATA里,我觉得还是编码的问题吧 <![CDATA[<Response><UserInfoSyncToISMPResp><StreamingNo></StreamingNo><ResultCode>-113</ResultCode><ResultDesc>参数错误</ResultDesc></UserInfoSyncToISMPResp></Response>]]>这是我返回的,木有看到么?先看清再回答 java 车牌号识别 混淆器的问题…… 怎样替换字符串 hibernate 分页配置文件 如果一个方法有返回类型,可以直接做为一个完整语句调用吗? 怎样像 Integer.parseInt(input.getText()); 一样得到double类型还有其他的类型的数字呢? 请问一般的小公司对面试人员有什么样的要求啊? 为什么有的错误连try{}catch(){}也抓不到? 全部的分求够JAVAMAIL教程下载的网址(最好的中文的)求求大家了急用 小白第一次发帖,遇难题 求解救 冒泡排序的结束条件是什么 把数据转成二进制数据
<![CDATA[<Response>
<UserInfoSyncToISMPResp>
<StreamingNo></StreamingNo>
<ResultCode>-113</ResultCode>
<ResultDesc>参数错误</ResultDesc>
</UserInfoSyncToISMPResp>
</Response>]]>那HttpURLConnection用得到的soap就为这样:
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><UserInfoSyncToISMPResp xmlns="http://www.mbossvsop.com.cn/vsop"><response xmlns=""><![CDATA[<Response><UserInfoSyncToISMPResp><StreamingNo></StreamingNo><ResultCode>-113</ResultCode><ResultDesc>参数错误</ResultDesc></UserInfoSyncToISMPResp></Response>]]></response></UserInfoSyncToISMPResp></soapenv:Body></soapenv:Envelope>
怎么出现这种情况
我的回答有误,不好意思,看看soap请求时有没有参数设置不转义,如果没的话只能自己解析了。
HttpURLConnection connection = null;
String soapAction = "userInfoSynSV";
String requestXML = getXML();
String serviceURL = "--------";
connection = openConnection(serviceURL, requestXML, soapAction);
InputStream input = connection.getInputStream();
StringBuffer buf = new StringBuffer();
byte[] b = new byte[1024];
int length = -1;
while ((length = input.read(b)) != -1) {
buf.append(new String(b, 0, length, "UTF-8"));
}
return buf.toString();
private static HttpURLConnection openConnection(String serviceURL,
String requestXML, String soapAction) throws Exception {
URL url = new URL(serviceURL);
URLConnection connection = url.openConnection();
String host =url.getHost();
HttpURLConnection httpConn = (HttpURLConnection) connection; // Open the
httpConn.setRequestProperty("Host", host);
httpConn.setRequestProperty("Content-Length", requestXML.length() + "");
httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
// httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", soapAction);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true); // Everything's set up; send the XML that was
OutputStream os = httpConn.getOutputStream();
os.write(requestXML.getBytes("utf-8"));
os.flush();
os.close();
return httpConn;
}
<UserInfoSyncToISMPResp>
<StreamingNo></StreamingNo>
<ResultCode>-113</ResultCode>
<ResultDesc>参数错误</ResultDesc>
</UserInfoSyncToISMPResp>
</Response>]]>这是我返回的,木有看到么?先看清再回答