在 C++Builder 下使用 Indy HTTP 下载位于代理后面的文件

问题描述 投票:0回答:1

我尝试使用 IdHTTP 通过代理下载文件时被阻止。会引发异常

EIdHTTPProtocolException
,并显示简单消息错误
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
,该错误对应于要下载的文件的第一行。

当我直接在浏览器中使用 URL(无论是否通过代理)时,下载工作正常。

下面是我的代码片段。有什么想法可以帮忙吗?

// 客户端:C++Builder 代码...

 TIdHTTP *IdHTTP1 = new TIdHTTP(this); TFileStream *FileStream1;
 try {FileStream1 = new TFileStream(myTempFileOnDisk, fmCreate);
      TIdSSLIOHandlerSocketOpenSSL *LHandler = new TIdSSLIOHandlerSocketOpenSSL(this);
      IdHTTP1->IOHandler = LHandler;
      IdHTTP1->HandleRedirects = true;

      IdHTTP1->Request->BasicAuthentication = true;
      IdHTTP1->Request->Username = "myUserName";
      IdHTTP1->Request->Password = "myBase64EncodedPassword";

      IdHTTP1->Request->UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0";
      //IdHTTP1->Get("https://www.myurl.com/myFileFolder/myFileToDownload.xml", FileStream1); // => Works fine !
      IdHTTP1->Request->ContentType = "text/xml; charset=\"UTF-8\"";
      IdHTTP1->Get("https://www.myurl.com/myProxy.php", FileStream1); // => Error

      myBoxMessage = "Success!";
      if(IdHTTP1->ResponseCode != 200) myBoxMessage = "Failure : "+ IdHTTP1->ResponseText +".";
      LHandler->Free();
     }
 catch(EIdHTTPProtocolException &EX0Vl) {myLogMessage = EX0Vl.Message; myLogFunction();}
 catch(EIdIOHandlerPropInvalid &EX0Vl) {myLogMessage = EX0Vl.Message; myLogFunction();}
 catch(...) {myLogMessage = "Exception non planifiée."; myLogFunction();}

 FileStream1->Free();

// 服务器端:PHP 代码 = 代理...

if ($_SERVER['PHP_AUTH_PW'] == "myBase64EncodedPassword")
    {try {$filename = (true)?"myFileToDownload.xml":"myFileToDownload.xlsx";
          $filepath = 'https://www.myurl.com/myFileFolder/'. $filename;
          if(fopen($filepath, 'r'))
            {header('Pragma: public');
             //header('Content-type: application/zip');
             header('Content-type: text/xml');
             //header('Content-Transfer-Encoding: Binary');
             header('Content-Transfer-Encoding: UTF-8');
             header('Content-Description: File Transfer');
             header('Content-Length: '. filesize($filename));
             header('Content-Disposition: attachment; filename='. $filename);
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Location: '. $filepath);
             header('Expires: 0'); 
             ob_clean(); flush();
             readfile($filepath);
             exit;
            } 
          else 
            {header('HTTP/1.1 404 Not found');
             error_log(date('Y-m-d H:i:s').", myProxy : File not found".PHP_EOL, 3, './myLogFile.log');   
             exit("File not found");
            }
         } 
     catch (Exception $EX0) 
        {header('HTTP/1.1 500 Internal Server Error');
         error_log(date('Y-m-d H:i:s').", myProxy : Internal Server Error".PHP_EOL, 3, './myLogFile.log');
         exit("Internal Server Error");
        }
    } 

// XML 文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ArticleTable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
</ArticleTable>
proxy c++builder indy idhttp
1个回答
0
投票
当 HTTP 服务器返回不成功的 HTTP 响应代码时,会引发

EIdHTTPProtocolException
。如果您的 PHP 脚本发送文件,它应该发送
200
响应代码。但您展示的 PHP 代码并没有这样做。这与客户使用的
ProtocolVersion
无关。

© www.soinside.com 2019 - 2024. All rights reserved.