同样的代码
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吗?