代码之家  ›  专栏  ›  技术社区  ›  er4z0r

如何区分HTTPURLConnection中的请求和响应?

  •  2
  • er4z0r  · 技术社区  · 16 年前

    但是,我不太清楚如何使用HTTPURLConnection实现这一点。特别是在查询我的连接对象的响应时。我目前:

    HttpURLConnection con = (HttpURLConnection) feedurl.openConnection();
        con.setRequestProperty("Accept-Encoding", "gzip");
        System.out.println("Current Accept-Encoding: "+con.getRequestProperty("Accept-Encoding"));
        //check the response for the content-size
        float feedsize = con.getContentLength()/1024f;
        //the server uses transfer-encoding=chunked and does not specify
        //a content length
        if(con.getContentLength()==-1)
        {
            //count the content size manually
                        CountingInputStream counter = new CountingInputStream(con.getInputStream());
            while(counter.read()!=-1)
            {}
            feedsize=counter.getCount()/1024f;
        }
        con.disconnect();
    
    1 回复  |  直到 16 年前
        1
  •  3
  •   Brian Agnew    16 年前

    看看 this OReilly article . 它说明了如何生成请求,以及如何查询响应,然后根据返回的内容创建适当的流(正常或gzip)。

    推荐文章