chengongkui7105 2016-09-22 02:07 采纳率: 0%
浏览 846

求大神帮忙,关于java报文转发的问题

我在写报文转发的时候,报了这个错误,Invalid Http response
请问是为什么?下面是代码
public static String postContent(String url, String content, String charSet) throws Exception {
OutputStream reqStream = null;
InputStream resStream = null;
URLConnection request = null;
String respText = null;
byte[] postData;
try {
postData = content.getBytes(charSet);
request = createRequest(url, "POST");

        request.setRequestProperty("Content-type", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        request.setRequestProperty("Content-length", String.valueOf(postData.length));
        request.setRequestProperty("Keep-alive", "ture");

        reqStream = request.getOutputStream();
        reqStream.write(postData);
        reqStream.close();

        ByteArrayOutputStream ms = null;
        resStream = request.getInputStream();  //这一行报错了
        ms = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int count;
        while ((count = resStream.read(buf, 0, buf.length)) > 0) {
            ms.write(buf, 0, count);
        }
        resStream.close();
        respText = new String(ms.toByteArray(), charSet);
    }
    catch (Exception ex) {
        throw ex;
    }
    finally {
        close(reqStream);
        close(resStream);
    }
    return respText;
}
  • 写回答

1条回答 默认 最新

  • C盘无限大 2016-09-22 02:55
    关注

    可定无效,你都没有请求服务器哪来的返回输入流,request要调用connect()方法才能获取他的输入流

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?