Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. String boundary = '-741d90e31frt';
  2. String header = '--'+boundary+'nContent-Disposition: form-data; name="pdfFile"; filename="'+Pdf_id+'.pdf";nContent-Type: application/octet-stream';
  3. String footer = '--'+boundary+'--';
  4. String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
  5. String bodyEncoded = EncodingUtil.base64Encode(pdf_Body);
  6.  
  7. while(headerEncoded.endsWith('=')){
  8. header+=' ';
  9. headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
  10. }
  11.  
  12. Blob bodyBlob= null;
  13. String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
  14. if(last4Bytes.endsWith('==')) {
  15. last4Bytes = last4Bytes.substring(0,2) + '0K';
  16. bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
  17. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  18.  
  19. // bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  20. }
  21. else if(last4Bytes.endsWith('=')) {
  22. last4Bytes = last4Bytes.substring(0,3) + 'N';
  23. bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
  24. footer = 'n' + footer;
  25. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  26.  
  27. //bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  28. }
  29. else if(last4Bytes.endsWith('9G')){
  30. system.debug('Entered in 9G');
  31. bodyEncoded = bodyEncoded +'Cg0K';
  32. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  33.  
  34. // bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  35. }
  36. else {
  37. footer = 'rn' + footer;
  38.  
  39. String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
  40.  
  41. // bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  42. }
  43.  
  44.  
  45. String header1 = '--'+boundary+'nContent-Disposition: form-data; name="signFile"; filename="'+image_id+'";nContent-Type: application/octet-stream'; //octet-stream
  46. String footer1 = '--'+boundary+'--';
  47. String headerEncoded1 = EncodingUtil.base64Encode(Blob.valueOf(header1+'rnrn'));
  48.  
  49. while(headerEncoded1.endsWith('=')){
  50. header1+=' ';
  51. headerEncoded1 = EncodingUtil.base64Encode(Blob.valueOf(header1+'rnrn'));
  52. }
  53. String bodyEncoded1 = EncodingUtil.base64Encode(image_Body);
  54. String last4Bytes1 = bodyEncoded1.substring(bodyEncoded1.length()-4,bodyEncoded1.length());
  55. if(last4Bytes1.endsWith('==')) {
  56. last4Bytes1 = last4Bytes1.substring(0,2) + '0K';
  57. bodyEncoded1 = bodyEncoded1.substring(0,bodyEncoded1.length()-4) + last4Bytes1;
  58. String footerEncoded1 = EncodingUtil.base64Encode(Blob.valueOf(footer1));
  59.  
  60. // bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  61. }
  62. else if(last4Bytes1.endsWith('=')) {
  63. last4Bytes1 = last4Bytes1.substring(0,3) + 'N';
  64. bodyEncoded1 = bodyEncoded1.substring(0,bodyEncoded1.length()-4) + last4Bytes1;
  65. footer1 = 'n' + footer1;
  66. String footerEncoded1 = EncodingUtil.base64Encode(Blob.valueOf(footer1));
  67.  
  68. //bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  69. }
  70. else if(last4Bytes1.endsWith('9G')){
  71. system.debug('Entered in 9G');
  72. bodyEncoded1 = bodyEncoded1 +'Cg0K';
  73. String footerEncoded1 = EncodingUtil.base64Encode(Blob.valueOf(footer1));
  74.  
  75. // bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  76. }
  77. else {
  78. footer1 = 'rn' + footer1;
  79.  
  80. String footerEncoded1 = EncodingUtil.base64Encode(Blob.valueOf(footer1));
  81.  
  82. // bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  83. }
  84.  
  85. String footerEncoded1 = EncodingUtil.base64Encode(Blob.valueOf(footer1));
  86.  
  87.  
  88.  
  89. bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+headerEncoded1+bodyEncoded1+footerEncoded1);
  90.  
  91.  
  92. HttpRequest requ = new HttpRequest();
  93. requ.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
  94.  
  95. requ.setMethod('POST');
  96. requ.setEndpoint(pointer.endpoint_url__c+'/xxxxxxx/XXXXX/XXX/XX');
  97.  
  98. String username = 'XXXXXX';
  99. String password = 'XXXXXX';
  100.  
  101. Blob headerValue = Blob.valueOf(username + ':' + password);
  102. String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
  103. requ.setHeader('Authorization', authorizationHeader);
  104.  
  105. requ.setBodyAsBlob(bodyBlob);
  106. requ.setTimeout(120000);
  107.  
  108. Http http = new Http();
  109. HTTPResponse res = http.send(requ);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement