[待解决]BEA/Weblogic: Exceeded stated content-length
同样的代码
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName");
在tomcat下好使,但是在WebLogic下就挂了,报错。
[code="java"]java.net.ProtocolException: Exceeding stated content length of 358
at weblogic.net.http.ContentLengthOutputStream.write(ContentLengthOutputStream.java:39)
at sun.nio.cs.StreamEncoder$CharsetSE.writeBytes(StreamEncoder.java:336)
at sun.nio.cs.StreamEncoder$CharsetSE.implFlushBuffer(StreamEncoder.java:404)
at sun.nio.cs.StreamEncoder$CharsetSE.implFlush(StreamEncoder.java:408)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:152)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:213)
出错的语句是:
[code="java"] OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
其中的358其实是
Integer.toString(soap.length())
这里有个帖子说到了解决方式
java.net.ProtocolException : Weblogic
里面提到不能设置长度,删除这句
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
但是为什么不能设呢,设的长度确实是soap的长度啊?
在soap里有汉字但是java和JavaScript对汉字都是1个,怎么会超长呢?
待研究下
PS:不知道WebLogic的get方式汉字编码是什么?tomcat不是iso-8859-1吗?
content-length这个是所传报文的byte类型的长度,而并非string字符串的长度,tomcat的编码格式iso-8859-1 ,而weblogic是GBK(或UTF-8) 这样就导致了超长。
WebLogic 是GBK
前两天也出现这问题了,最后发现content-length这个是所传报文的byte类型的长度,而并非string字符串的长度,如果内容中有中文,就不对了,调用getBytes方法可解决