yaomint 发表于 2017-2-18 09:50:37

关于weblogic下Did not meet stated content length of OutputStream异常

  这段时间做上海公共地理信息服务平台的时候,卡在了webservice在C#中的调用上,因为我们的服务发布要进行控制,所以就做了个代理,在代理里分析了当前服务的权限后再经过后台请求转发原服务地址,代码如下:
  public Document loadByUrlAndDoc(HttpServletRequest req, String url, Document doc) {
  Document resultDoc = null;
SAXReader saxReader = new SAXReader();
HttpURLConnection connection = null;
InputStream cin = null;
OutputStream out = null;
XMLWriter writer = null;
try {
  URL httpURL = new URL(url);
connection = (HttpURLConnection) httpURL.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);

if(doc != null){
connection.setRequestMethod("POST");
connection.setRequestProperty("SOAPAction",
"http://www.example.org/TopoAnalyse/DownStream");
}
else{
connection.setRequestMethod("GET");
}

connection.setRequestProperty("", "");
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);

  //此处将表头中的所有属性都加载到转发请求中了
for(Enumeration enum1 = req.getHeaderNames(); enum1.hasMoreElements(); ){
String header = (String)enum1.nextElement();
String value = req.getHeader(header);
connection.setRequestProperty(header, value);
log.info("header:"+header+" value:"+value);
}

if(doc != null){

out = connection.getOutputStream();
  OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
  writer = new XMLWriter(out, format);
writer.write(doc);
writer.flush();

}

cin = connection.getInputStream();
  BufferedInputStream bci = new BufferedInputStream(cin);
resultDoc = saxReader.read(bci);
  } catch (Exception e) {
log.info(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.info(e);
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
log.info(e);
}
}
  if (cin != null) {
try {
cin.close();
} catch (IOException e) {
log.info(e);
}
}

if(connection != null) {
connection.disconnect();
}
}
return resultDoc;
}
  之后再WebLogic中报以下错误(在tomcat中没问题)
  Did not meet stated content length of OutputStream: you wrote 0 bytes and I was expecting you to write exactly 187 bytes!!!
分析了很久最后发现是在装载表头的时候把ContentLength也设置进去了
  最后将contentlength从表头中去除就OK乐
  去掉表头中contentlength的方法如下:
  for(Enumeration enum1 = req.getHeaderNames(); enum1.hasMoreElements(); ){
String header = (String)enum1.nextElement();
String value = req.getHeader(header);
   if(header!="content-length")
connection.setRequestProperty(header, value);
log.info("header:"+header+" value:"+value);
}
  还有一种方法,如下:
  请检查web应用中的servlet代码查看是否设置了长度。也可以做下面的修改来解决这个问题:
  response.setContentLength(0);
  ……
  ……
  response.flushBuffer();
  return;
页: [1]
查看完整版本: 关于weblogic下Did not meet stated content length of OutputStream异常