我根據提供的檔案API URL和一些人parameters一起打電話。
API 的設定方式,回應應該是.pdf檔案的自動下載。
引數是:
number- 代表一個 13 位數字,代表他們系統中的唯一檔案指示符(例如:5277110610029)username和user_pass分別 - 代表訪問系統的登錄憑據client_id- 代表與我在他們系統中的帳戶相關聯的唯一客戶 IDlanguage- 我指出檔案內容應該使用的語言(英語或其他可用語言)
檔案表明它應該作為Post請求提出。
我有以下代碼:
var
FileName : string;
URL: String;
Params: TStringList;
memStream: TMemoryStream;
...
begin
FileName:= MainModule.PDFfileName '.pdf';
begin
URL := 'https://someURL/view_integrated_pdf.php?';
Params := TStringList.Create;
memStream := TMemoryStream.Create;
try
Params.Add('number=' MainModule.number '');
Params.Add('username=' MainModule.User '');
Params.Add('client_id=' MainModule.clientID '');
Params.Add('user_pass=' MainModule.Pass '');
Params.Add('language=en');
MainModule.IdHTTP.Post(URL, Params, memStream);
finally
memStream.SaveToFile(ServerModule.FilesFolderPath '\pdfs\' FileName);
Params.Free;
memStream.Free;
end;
end;
pdfForm.ShowModal();
end;
If I try the resulting URL and parameters in the browser - it auto-downloads the pdf file the API gives me with the name numberParameter.pdf
If I do it in Delphi using the provided code, 8 out of 10 times it saves a pdf file with a 1 KB Size (normally it is between 32 and 100 for the successful file) and when I try to open it in the program using my pdfForm and subsequent viewer, the viewer throws an error "Invalid PDF structure"
- What am I doing wrong? / How do you properly save a Post requests that returns a .pdf file to download from the API?
UPDATE
As per the comments, opening up the 1kb resulting PDF in Notepad displays the contents as simply Error username.
This is puzzling since I checked and the username being passed is accurate if I paste the exact same URL with parameter values filed, in the browser, it works perfectly and gives me the correct PDF.
- Is my code not the correct approach to Post and save the file being sent?
uj5u.com熱心網友回復:
如果我在瀏覽器中嘗試結果
URL和parameters- 它會自動下載numberParameter.pdf
在瀏覽器中執行此操作的唯一方法是將引數放在 URL 的查詢組件中,例如:
https://someURL/view_integrated_pdf.php?number=...&username=...&client_id=...&user_pass=...&language=en
......而不是POST像你的代碼那樣在一個身體中。此外,瀏覽器會發送GET請求,而不是POST請求。除非您填寫 HTML 網路表單(即<form action="<URL>" method="POST" ...>)并提交。這是API檔案所說的嗎?
由于您沒有從檔案中提供有關此服務器實際期望的任何詳細資訊,我們無法真正告訴您您的代碼是正確還是錯誤。但是,您的手動測驗似乎與您聲稱的檔案所說的服務器期望的內容相矛盾。
如果我使用提供的代碼在 Delphi 中執行此操作,則 10 次中有 8 次它會保存一個大小為 1 KB 的 pdf 檔案(成功檔案的大小通常在 32 到 100 之間),并且當我嘗試在程式中使用我的 pdfForm 和隨后的查看器,查看器拋出錯誤“無效的 PDF 結構”
從評論中,您說您的檔案收到了文本錯誤訊息。 只有在以下情況下TIdHTTP才會將此類文本保存到:TMemoryStream
HTTP 服務器沒有在 HTTP 級別報告錯誤,即它正在使用 HTTP
2xx成功回應發送文本訊息。我懷疑這就是你的情況。默認情況下,如果服務器使用正確的HTTP 錯誤代碼,TIdHTTP將引發EIdHTTPProtocolException包含文本訊息的訊息,并且根本不會將文本保存到您TMemoryStream的。HTTP服務器IS報告在HTTP級別的錯誤,但您使用的是
hoNoProtocolErrorException與hoWantProtocolErrorContent在標志一起TIdHTTP.HTTPOptions財產。在這種情況下,TIdHTTP不會引發EIdHTTPProtocolException而是將服務器按原樣保存到您的TMemoryStream.
由于顯然沒有引發 HTTP 例外,因此您必須先驗證服務器的回應,然后才能在 .h 中使用下載的資料pdfForm,即通過查看TIdHTTP.Response.ContentType和/或TIdHTTP.Response.ContentDisposition屬性來了解服務器是否實際發送了 PDF 檔案。
這是令人費解的,因為我檢查過并且傳遞的用戶名是準確的 如果我粘貼與提交的引數值完全相同的 URL,在瀏覽器中,它可以完美運行并為我提供正確的 PDF。
嗯,一方面,您的代碼中有一個錯字:該numberr欄位需要number改為。
除此之外,將 URL 放入瀏覽器與您的代碼正在執行的操作不同,因此請嘗試更改代碼以模仿您手動執行的操作,例如:
uses
..., IdGlobalProtocols, IdHTTP, IdURI;
...
var
URL : string;
memStream: TMemoryStream;
begin
// All parameters into the URI for a HTTP GET request
URL := 'https://someURL/view_integrated_pdf.php'
'?number=' TIdURI.ParamsEncode(MainModule.number)
'&username=' TIdURI.ParamsEncode(MainModule.User)
'&client_id=' TIdURI.ParamsEncode(MainModule.clientID)
'&user_pass=' TIdURI.ParamsEncode(MainModule.Pass)
'&language=en';
memStream := TMemoryStream.Create;
try
MainModule.IdHTTP.Get(URL, memStream);
// Is it really PDF? Other formats such as plaintext is not wanted.
if not IsHeaderMediaType(MainModule.IdHTTP.ContentType, 'application/pdf') then Exit;
memStream.SaveToFile(ServerModule.FilesFolderPath '\pdfs\' MainModule.PDFfileName '.pdf');
finally
memStream.Free;
end;
pdfForm.ShowModal;
end;
If that does not work, then please update your question to provide the actual documentation.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/340924.html
上一篇:為什么FileAge回傳意外值?
