2014-12-05 80 views
1

簽約使用Web代理PDF時,當測試在Java Web代理樣品,我得到一個錯誤的回覆錯誤代碼706的Java

<?xml version="1.0" encoding="utf-8"?> 
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="error"> 
    <Error> 
    <returnCode>706</returnCode> 
    <errorMessage>Value cannot be null. 
Parameter name: s</errorMessage> 
    </Error> 
</response> 

我跟着餘弦Web代理樣本和documentation在Ruby例子

我已經使用樣本中提供的demo.pdf文件。

這是在POST請求中發送的XML(從test app)(<content></content>具有Base64編碼的PDF,但由於長度而被省略)。

<?xml version="1.0" encoding="utf-8" ?> 
<request> 
    <Logic> 
    <allowAdHoc>true</allowAdHoc> 
    <workingMode>pull</workingMode> 
    <enforceReason>false</enforceReason> 
    </Logic> 
    <Url> 
    <finishURL>http://localhost:64956/retrieveSignedFile.aspx</finishURL> 
    </Url> 
    <Document> 
    <fileID>1234567890</fileID> 
    <contentType>pdf</contentType> 
    <content>{BASE64 encoded pdf content}</content> 
    </Document> 
</request> 

以下是Java代碼我已經使用:

public class CoSignTest { 
    private static final String INPUT = "D:\\tmp\\demo.pdf"; 
    private static final String PRECONTENT = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + 
      "<request>\n" + 
      " <Logic>\n" + 
      " <allowAdHoc>true</allowAdHoc>\n" + 
      " <workingMode>pull</workingMode>\n" + 
      " <enforceReason>false</enforceReason>\n" + 
      " </Logic>\n" + 
      " <Url>\n" + 
      " <finishURL>http://localhost:64956/retrieveSignedFile.aspx</finishURL>\n" + 
      " </Url>\n" + 
      " <Document>\n" + 
      " <fileID>1234567890</fileID>\n" + 
      " <contentType>pdf</contentType>\n" + 
      " <content>"; 
    private static final String POSTCONTENT = "</content>\n" + 
      " </Document>\n" + 
      "</request>"; 
    private static final String POST_URL = "https://webagentdev.arx.com/Sign/UploadFileToSign"; 
    private static final String PULL_URL = "https://webagentdev.arx.com/Sign/DownloadSignedFileG"; 
    public static final int TIMEOUT = 300000; 

    public static void main(String[] args) throws Exception { 
     InputStream is = new FileInputStream(INPUT); 
     String content = PRECONTENT + new String(Base64.encodeBase64(loadResource(is)), "UTF-8") + POSTCONTENT; 
     System.out.println(content); 
     String reply = new String(sendDocForProcessing(URLEncoder.encode(content, "UTF-8"))); 
     System.out.println(reply); 
     System.out.println("DONE"); 
    } 

    private static String sendDocForProcessing(String content) throws Exception { 
     HttpClient client = null; 
     HttpMethodBase method = null; 
     SimpleHttpConnectionManager mgr = new SimpleHttpConnectionManager(); 
     String reply = ""; 
     try { 
      mgr.getParams().setConnectionTimeout(TIMEOUT); 
      mgr.getParams().setSoTimeout(TIMEOUT); 
      client = new HttpClient(mgr); 
      method = new PostMethod(POST_URL); 
      method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false)); 
      method.getParams().setParameter("http.socket.timeout", TIMEOUT); 
      client.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT); 
      client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 
      method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
      method.getParams().setParameter("inputXML", content); 
      client.executeMethod(method); 
      reply = new String(method.getResponseBody()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if(method != null) { 
       method.releaseConnection(); 
      } 
      client = null; 
      mgr.shutdown(); 
     } 
     if (isSigningSuccessful(reply)) { 
      return reply; 
     } else { 
      throw new Exception("Failed in signing the document. Error: " + reply); 
     } 
    } 

    private static boolean isSigningSuccessful(String reply) throws ParserConfigurationException, IOException, SAXException { 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document doc = db.parse(new ByteArrayInputStream(reply.getBytes())); 
     Element elem = doc.getDocumentElement(); 
     String type = elem.getAttribute("type"); 
     return !"error".equals(type); 
    } 


    public static byte[] loadResource(InputStream in) { 
     if (in == null) { 
      return new byte[0]; 
     } 
     try { 
      int indice, tempIndice; 
      byte[] tempArr; 
      byte[] mainArr = new byte[0]; 
      byte[] byteArr = new byte[65535]; 
      for (indice = 0; (indice = in.read(byteArr)) > 0;) { 
       tempIndice = mainArr.length + indice; 
       tempArr = new byte[tempIndice]; 
       System.arraycopy(mainArr, 0, tempArr, 0, mainArr.length); 
       System.arraycopy(byteArr, 0, tempArr, mainArr.length, indice); 
       mainArr = tempArr; 
      } 
      in.close(); 
      return mainArr; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return new byte[0]; 
    } 
} 
+0

您可以發佈您發送給*** Web代理的XML嗎? – 2014-12-06 17:00:08

回答

0

感謝您添加您的Java代碼。請注意,HttpClient實例配置不正確,因此http-post請求被髮送爲空。看看我在你sendDocForProcessing功能,以便正確地發佈XML內容做了修改:

private static String sendDocForProcessing(String content) throws Exception { 
    HttpClient client = null; 
    PostMethod method = null; 
    String reply = ""; 
    try { 
     client = new HttpClient(); 
     method = new PostMethod(POST_URL); 
     method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
     NameValuePair[] data = { new NameValuePair("inputXML", content) }; 
     method.setRequestBody(data); 
     client.executeMethod(method); 
     reply = method.getResponseBodyAsString(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if(method != null) { 
      method.releaseConnection(); 
     } 
    } 
    if (isSigningSuccessful(reply)) { 
     return reply; 
    } else { 
     throw new Exception("Failed in signing the document. Error: " + reply); 
    } 
} 

通過上述功能的內容不應該被URL編碼,因爲它已經由HttpClient庫完成。

另外,在分析響應時,我建議您檢查returnCode元素的值,而不是type屬性。響應總是輸入'錯誤'。 另請注意,功能名稱isSigningSuccessful具有誤導性,因爲此階段仍處於簽名行爲之前。

+0

非常感謝,Almog G.您的建議有效。 – 2014-12-24 11:44:13

1

的XML元素是大小寫敏感的並且如圖所示在documentation(例如Document代替document必須被傳遞,Auth代替auth等等)。另外,您的XML請求缺少必需的finishURL參數。

另請注意,XML請求中的某些參數已過時。請參閱上面鏈接中更新的請求參數列表。示例XML可用here

+0

Almog 即使在更改請求XML之後,響應也是一樣的。請求XML已在該問題中更新。 – 2014-12-10 05:16:44

+0

這是一個有用的應用程序來測試您的XML請求 - http://webagentdev.arx.com:88/testApp/ – 2014-12-11 09:44:19

+0

您的XML請求似乎很好。確保在發佈請求之前對您的XML進行URL編碼。 – 2014-12-11 10:03:55