2013-03-22 124 views
0

我必须从tsa服务器获取时间戳。我正在发送一个文件(转换为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抛出一个

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

我不知道问题出在TSA服务器或在我的代码。任何人都可以帮助我?

+0

我忘了说,leerByteFichero函数只是读一个文件并将其转换为一个字节[] – Ainur 2013-03-22 08:57:48

回答

1

我可以看到的问题在于你的请求(NullPointer来自空的resp,因为你没有回应)。具体来说,问题是你的HTTP请求头后没有冒号。这会使服务器无法读取强制性的Content-length标头。从RFC2616第4.2节(HTTP DOC):

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:'TimeStampResp'工厂中的未知对象:org.bouncycastle.asn1.DERUnknownTag创建TimeStampResp对象 – Ainur 2013-03-22 09:57:01

+1

+1此答案解决了最初的问题,对于新问题也许它是最好再问一个问题。 – dcernahoschi 2013-03-22 10:11:25