0
votes

Friends, this is my first question here... I've been facing some problems when downloading a PDF buffer using MS2XML.XMLHTTP. I've been using Genexus to do so but I also tried right in pure Visual Fox Pro. The problem is that when I send the ResponseText to a string variable, some characters are replaced by question marks, the sam happens when I send the ResponseText to a pdf or txt file. The object created in MS2XML.XMLHTTP.6.0 does not allow using the ResponseBody property. Any thoughts on how could I solve it using MS2XML.XMLHTTP? Thanks.

oHTTP = CreateObject("MSXML2.XMLHTTP.6.0")  
oHTTP.Open("GET", 'https://homologacao.plugboleto.com.br/api/v1/boletos/impressa  /lote/NIKLfYBWz',.F.)
oHTTP.setRequestHeader("content-type", "application/pdf")
oHTTP.Send()
? oHTTP.responseText

I've received someething like the following (full of question marks):

%PDF-1.4 %??2 0 obj <</ColorSpace/DeviceRGB/Subtype/Image/Height 38/Filter/DCTDecode/Type/XObject/Width 149/BitsPerComponent 8/Length 2619>>stream ???JFIF H H ??C !"$"$??C?? & ?" ?? ??? } !1AQa"q2???#B??R?$3br? %&'()456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz???????????????????????????????????????????????????? ??? w !1AQaq"2?B????#3R??$4?&'()56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz??????????????????????????????????????????????????? ? ?h???8OO????a&??G?3? ?p1???|b?o?? ??x?_???%??E?en9??T???T>.????JG??rx??????????h?w????????:?!?????????jlm?Tn???????u??? ??Ey?PA?? (?? (?? (?? (??>B???;.??3?e??J??~?F??? y,s??i???#?m=kw???? ?[?K#????vR@G??^$?????k?[??BSu??#???M??????? _??Z?Fo??????/??*x?¾ mn??{)???80??s]W?x? ??+??k??=????????8 ?|D?c?j???h???$?8???:c??(???M/?Ze??;O?[?J????? '?~/j~!???n?urm???1^ITl;?3?%[?b??~?&<=u?Y\x??W6?¬$2?q?1?;??qc??_??qk>?&?v?????,??F?{??x???s??????{?k????r8.??<P?|,????q?]I?]?e???p;??/?W?x??)???A?????&)??dc,?d7?J?s??m?>???????!??9Va?? c???Zv??x+?b?wd??f?8a????????,6????????x?? ?-????<9F???????[~$?{??o???X??????y?ZgQ?@8??ox;? ???|??mZ?? I~a?k ~P ?? j??? '?c??4?F??l??$?8???(?'?"?.?????,????9V?????d???????UU)??? ???o?&???4?7?Z?? g???y? W[?????d?Q$?@??^mZ???B Z(??QE ?Q@Q@Q@Q@Q@??endstream endobj 6 0 obj <</Filter/FlateDecode/Length 846>>stream x??V???+?q??z???'U./h???{???d?U??xf??PQ?O???unA???x1?0 50]@?\?T?y?B?s?9B? ? 2|????C???2t????k?U??]??]{? ?????s?AH??????h?"w????? f?????i??? ??>?9?8?#??"??G?$???<??0???S?2??sn?n??^?5?\FN?o1?4?~4~??Qe=&?T[???????Z??x??????k?????0z'#?;?'a??a?f~?q?~8ZH~?m???????Mm?p?@hh{????W7?????? ?8?Olk'?A|?[???P?5?????uGxRr#?pw<$y?n??kD ???0??ih??9?5v??0?_}iG?Dq?8??_U??5a?????k????d???M??2???C

1

1 Answers

2
votes

Since a PDF is a binary file and not a text file, it is quite normal you would see ? and all sorts of other non-printable characters. Instead save it to a file on disk and open with something like ShellExecute. ie:

oHTTP = CreateObject("MSXML2.XMLHTTP.6.0")  
oHTTP.Open("GET", 'https://homologacao.plugboleto.com.br/api/v1/boletos/impressa  /lote/NIKLfYBWz',.F.)
oHTTP.setRequestHeader("content-type", "application/pdf")
oHTTP.Send()

Local lcFileName
lcFileName = Forcepath(Sys(2015)+'.pdf', Sys(2023))
Strtofile(oHttp.responseText, m.lcFileName)

Declare Long ShellExecute In "shell32.dll" ;
    long HWnd, String lpszOp, ;
    string lpszFile, String lpszParams, ;
    string lpszDir, Long nShowCmd

ShellExecute(_vfp.HWnd,'',m.lcFileName,'','',1)

EDIT: It was not a job MSXML2.XmlHttp. You simply download the file as a PDF and open it:

Local lcFileName, lcRemote
lcRemote = 'https://homologacao.plugboleto.com.br/api/v1/boletos/impressao/lote/NIKLfYBWz'

lcFileName = Forcepath(Sys(2015)+'.pdf', Sys(2023))

If (getFileFromURL(m.lcRemote, m.lcFileName) = 0)
    Declare Long ShellExecute In "shell32.dll" ;
        long HWnd, String lpszOp, ;
        string lpszFile, String lpszParams, ;
        string lpszDir, Long nShowCmd

    ShellExecute(_vfp.HWnd,'',m.lcFileName,'','',1) 
Endif

Procedure getFileFromURL
    Lparameters tcRemoteFile,tcLocalFile
    Declare Integer URLDownloadToFile In urlmon.Dll;
        INTEGER pCaller, String szURL, String szFileName,;
        INTEGER dwReserved, Integer lpfnCB
    Return URLDownloadToFile(0, m.tcRemoteFile, m.tcLocalFile, 0, 0)
endproc