2013-03-22 3 views
0

TSS 서버에서 타임 스탬프를 얻어야합니다. 파일을 보내고 있습니다 (byte[]로 변환).
하지만 응답을 얻으려고하면 NullPointerException이 나에게 걸립니다.타임 스탬프 응답의 오류

public static void timeStampServer() throws IOException{ 
     //String TSA_URL1 = "http://tsa.starfieldtech.com/"; 
     String TSA_URL2 = "http://ca.signfiles.com/TSAServer.aspx"; 
     //String TSA_URL3 = "http://timestamping.edelweb.fr/service/tsp"; 
     try { 
      byte[] digest = leerByteFichero("C:\\deskSign.txt"); 

      TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator(); 
      TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA1, digest); 
      byte request[] = req.getEncoded(); 

      URL url = new URL(TSA_URL2); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 

      con.setDoOutput(true); 
      con.setDoInput(true); 
      con.setRequestMethod("POST"); 
      con.setRequestProperty("Content-type", "application/timestamp-query"); 

      con.setRequestProperty("Content-length", String.valueOf(request.length)); 

      if (con.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage()); 
      } 
      InputStream in = con.getInputStream(); 
      TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject()); 
      TimeStampResponse response = new TimeStampResponse(resp); 
      response.validate(req); 
      System.out.println(response.getTimeStampToken().getTimeStampInfo().getGenTime()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

임 3 TSA 서버와 노력하지만 그들 중 하나가 나에게 유효한 TSA를 반환

내 코드입니다.

의 " TimeStampResponse response = new TimeStampResponse(resp);"에 모두 NullPointerException이 발생합니다.

TSA_URL2 내가 문제가 TSA 서버 또는 내 코드에 있는지 잘 모릅니다

java.io.IOException: Received HTTP error: 411 - Length Required.

가 발생합니다. 누구든지 나를 도울 수 있습니까?

+0

는 그 leerByteFichero 기능은 단지 판독 대답 깜빡 파일을 만들고 바이트로 변환하십시오. – Ainur

답변

1

내가 볼 수있는 문제는 요청에 있습니다 (NullPointer는 응답이 없으면 빈 resp에서 유래합니다). 특히 문제는 HTTP 요청 헤더 뒤에 콜론이 없다는 것입니다. 이렇게하면 서버가 필수 Content-Length 헤더를 읽지 못하게됩니다. RFC2616 4.2 (HTTP의 문서)에서 :

HTTP header fields, which include general-header (section 4.5), request-header (section 5.3), response-header (section 6.2), and entity-header (section 7.1) fields, follow the same generic format as that given in Section 3.1 of RFC 822. Each header field consists of a name followed by a colon (":") and the field value.

TL; DR :

변화율 :

 con.setRequestProperty("Content-type", "application/timestamp-query"); 
     con.setRequestProperty("Content-length", String.valueOf(request.length)); 

행 :

 con.setRequestProperty("Content-type:", "application/timestamp-query"); 
     con.setRequestProperty("Content-length:", String.valueOf(request.length)); 
+0

당신이 옳았다는 것이 문제입니다. 이제 java.lang.IllegalArgumentException : java.lang.IllegalArgumentException : 'TimeStampResp'팩토리의 알 수없는 객체 : TimeStampResp 객체를 생성 할 때 org.bouncycastle.asn1.DERUnknownTag – Ainur

+1

+1이 답변은 새로운 문제에 대해 초기 질문을 해결합니다. 다른 질문을하는 것이 좋습니다. – dcernahoschi