【问题标题】:How to download XML content generated by PHP script using WebClient?如何使用 WebClient 下载 PHP 脚本生成的 XML 内容?
【发布时间】:2010-09-01 16:54:56
【问题描述】:

我有一个 PHP 脚本,它使用“echo”命令和数据库中的数据生成 XML。 所以,如果我从浏览器访问这个脚本,我可以看到 xml 数据并下载它。

所以我编写了一个软件来使用 webclient 类检索这些数据。但是 webclient 只下载一个空文件,所以我认为它是在尝试下载 .php 文件,而不是生成的动态内容。

PHP 脚本正在发送一个header("Content-type: text/xml") 并且网络客户端尝试从https://mySecureServer.com/db/getXMLData.php 下载(也许这就是问题所在)。

有什么想法吗?

编辑:WebClient 代码(只是翻录了一些本地文件操作):

string url = @"https://mySecureServer.com/db/getXMLData.php";
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompletedEvtHdl);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChangedEvtHdl);
client.BaseAddress = @"https://mySecureServer.com/db/";
client.DownloadFileAsync(new Uri(url), toSavePath + filename);

【问题讨论】:

  • Webclient 无法下载 .php 文件,因为它永远不可用。
  • 请出示您用于下载的代码。

标签: c# php xml download


【解决方案1】:

如果你只是想下载这个getXMLData.php脚本正在生成的XML数据,那么你可以直接使用cURL来获取它的内容:

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_SSL_VERIFYPEER => false     // disable certificate checking
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

//Now get the webpage
$data = get_web_page( "https://mySecureServer.com/db/getXMLData.php" );

//Display the data (optional)
echo "<pre>" . $data['content'] . "</pre>";

【讨论】:

  • 尝试了您的代码,但得到了这个:“指定的 CGI 应用程序由于未返回一组完整的 HTTP 标头而行为异常。它确实返回的标头是: ,这似乎是 IIS+PHP (bugs.php.net/bug.php?id=25863) 的一个相当常见的错误。您还有其他想法或解决方法吗?
猜你喜欢
  • 2010-10-25
  • 2015-12-06
  • 2020-01-25
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
相关资源
最近更新 更多