2016-10-03 4 views
3

다음 코드를 사용하여 HTTP 헤더를 인쇄하고 있습니다. 이제HttpURLConnection이 모든 헤더를 반환하지 않습니다.

Key : null ,Value : [HTTP/1.1 200 OK] 
Key : Accept-Ranges ,Value : [bytes] 
Key : Cache-Control ,Value : [max-age=604800, public] 
Key : Connection ,Value : [Keep-Alive] 
Key : Date ,Value : [Mon, 03 Oct 2016 18:01:06 GMT] 
Key : ETag ,Value : ["159eb4-53dce1f957880-gzip"] 
Key : Expires ,Value : [Mon, 10 Oct 2016 18:01:06 GMT] 
Key : Keep-Alive ,Value : [timeout=5, max=100] 
Key : Last-Modified ,Value : [Sat, 01 Oct 2016 13:59:46 GMT] 
Key : Server ,Value : [Apache/2.4.12 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4] 
Key : Transfer-Encoding ,Value : [chunked] 
Key : Vary ,Value : [Accept-Encoding,User-Agent] 

나는 컬을 통해 노력하고있어 다음 Content-Length 헤더는 자바 출력에서 ​​누락되는 방법을

$ curl -v -D - http://example.com/example -o /dev/null 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0* Trying 111.111.111.111... 
    0  0 0  0 0  0  0  0 --:--:-- 0:00:01 --:--:--  0* Connected to example.com (111.111.111.111) port 80 (#0) 
> GET example.com/example HTTP/1.1 
> Host: example.com 
> User-Agent: curl/7.47.0 
> Accept: */* 
> 
< HTTP/1.1 200 OK 
HTTP/1.1 200 OK 
< Date: Mon, 03 Oct 2016 18:25:11 GMT 
Date: Mon, 03 Oct 2016 18:25:11 GMT 
< Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 
Server: Apache/2.4.12 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 
< Last-Modified: Sat, 01 Oct 2016 13:59:46 GMT 
Last-Modified: Sat, 01 Oct 2016 13:59:46 GMT 
< ETag: "159eb4-53dce1f957880" 
ETag: "159eb4-53dce1f957880" 
< Accept-Ranges: bytes 
Accept-Ranges: bytes 
< Content-Length: 1416884 
Content-Length: 1416884 
< Cache-Control: max-age=604800, public 
Cache-Control: max-age=604800, public 
< Expires: Mon, 10 Oct 2016 18:25:11 GMT 
Expires: Mon, 10 Oct 2016 18:25:11 GMT 
< Vary: Accept-Encoding,User-Agent 
Vary: Accept-Encoding,User-Agent 

< 
{ [1080 bytes data] 
30 1383k 30 427k 0  0 116k  0 0:00:11 0:00:03 0:00:08 116k^C 

참조 여기에

URL url = new Url("htttp://example.com/example"); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

    Map<String, List<String>> map = conn.getHeaderFields(); 
    for (Map.Entry<String, List<String>> entry : map.entrySet()) { 
     System.out.println("Key : " + entry.getKey() + 
       " ,Value : " + entry.getValue()); 
    } 

출력입니까? 왜 이런 일이 생길까요? 이 문제를 어떻게 해결할 수 있습니까?

+0

그것은 API를 통해 제공되는 것 :

이 내가 무엇을 얻을 수 있습니다. 수정 프로그램이없고 필요하지 않습니다. – EJP

+0

@EJP 어떻게 수정해야합니까? 서버가'Content-Length'를 보내면 API가'Content-Length'를 반환하지 않아야합니까? –

+0

내가 참조하는 API는'getContentLength()'(및'getContentLengthLong()')입니다. – EJP

답변

2

'Content-Length'는 자동으로 구문 분석되며 conn.getContentLength() 또는 conn.getContentLengthLong()을 통해 검색 할 수 있습니다. 자세한 내용은 https://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html#getContentLengthLong()을 참조하십시오. 서버가 응답을 청크로 전송했기 때문에

+0

'content-length'가 헤더에없는 이유를 설명하지 않습니다. 헤더가 존재하지 않으면'-1'을 반환합니다. –

+0

@NicolasFilotto 헤더가 없다면 -1을 반환합니다 't * sent *,'curl' 결과에 따라 * 여기서는 그렇지 않습니다. – EJP

+0

@EJP 아니오'www.google.com'을 곱슬 곱슬로 테스트했습니다.'Content-Length'는 258이지만 java를 사용하면이 헤더는 목록에 포함되지 않으므로이 메서드를 호출하면 -1이됩니다. –

4

Content-Length이 누락되었습니다. 그것은 예상 된 행동입니다. Java 응답의 Transfer-Encoding 헤더를보고 여기에 RFC 2616, 4.4 Message Length이 있습니다 (목록의 세 번째 항목 참조).

+0

그래서 이것을 어떻게 비활성화합니까? –

+0

@BinoyBabu 그건 불가능합니다. 이 [관련 질문] (http://stackoverflow.com/questions/18174435/avoiding-chunked-encoding-of-http-1-1-response)을 참조하십시오. [RFC 2616, 3.6.1] (https://tools.ietf.org/html/rfc2616#section-3.6.1)에는'All HTTP/1.1 응용 프로그램은 "chunked"전송 코딩을 수신하고 디코딩 할 수 있어야합니다. 따라서이 헤더에 대처해야합니다. – nandsito

+0

@BinoyBabu [RFC 2616, 14.41] (https://tools.ietf.org/ html/rfc2616 # section-14.41)''많은 이전 HTTP/1.0 응용 프로그램은 Transfer-Encoding 헤더를 이해하지 못합니다. '라고 말하며 서버를 구성 할 수 있고 실제로'Transfer-Encoding'을 비활성화하고자한다면 서버를 롤백 할 수 있습니다 HTTP/1.0으로 바꾸지 만,'Transfer-Encoding '그 자체와 같은 1.1 개선점을 많이 잃을지도 모른다고 강력하게 권고하지 않는다. – nandsito

0

자바의 경우 응답은 컬링 응답의 경우 누락 된 'transfer-encoding'헤더를 포함합니다. 또한 내가 그것을 시도했을 때, 나는 나의 시도를 위해 404 (찾지 못했다)를 얻었다. 내 브라우저에서도 시도 할 때 404가 표시됩니다.

내 생각 엔 자신의 바람둥이 등으로 돌아가는 example.com에 대한 재정의 호스트 항목이있는 것 같습니다. 귀하의 환경과 관련된 문제입니다.

/** 
    * 
    * <P> jdk-1.8 </P> 
    * <PRE> 
Key :null || Values :[HTTP/1.1 404 Not Found] 
Key :X-Cache || Values :[HIT] 
Key :Server || Values :[ECS (ewr/1445)] 
Key :Etag || Values :["359670651+gzip+ident"] 
Key :Cache-Control || Values :[max-age=604800] 
Key :x-ec-custom-error || Values :[1] 
Key :Vary || Values :[Accept-Encoding] 
Key :Last-Modified || Values :[Fri, 09 Aug 2013 23:54:35 GMT] 
Key :Expires || Values :[Mon, 10 Oct 2016 19:20:29 GMT] 
Key :Content-Length || Values :[1270] 
Key :Date || Values :[Mon, 03 Oct 2016 19:20:29 GMT] 
Key :Content-Type || Values :[text/html] 
    * </PRE> 
    * @param urlStr (http://example.com/example) 
    */ 
    private static void printHeaders(String urlStr) { 

     try { 
      URL urlRef = new URL(urlStr); 
      URLConnection urlConnection = urlRef.openConnection(); 
      Map<String, List<String>> headerFields = urlConnection.getHeaderFields(); 

      for (String headerName : headerFields.keySet()) { 
       List<String> headerEntry = headerFields.get(headerName); 
       System.out.println("Key :"+headerName +" || Values :"+headerEntry); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
관련 문제