2014-12-05 78 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