我必須從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服務器或在我的代碼。任何人都可以幫助我?
我忘了說,leerByteFichero函數只是讀一個文件並將其轉換爲一個字節[] – Ainur 2013-03-22 08:57:48