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

本文介绍了解决在WebLogic环境下调用WebService时遇到的Content-Length不匹配问题,通过排除Content-Length头信息或显式设置为0来避免异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这段时间做上海公共地理信息服务平台的时候,卡在了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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值