如何使用WebClient下载PHP脚本生成的XML内容?

时间:2010-09-01 16:54:56

标签: c# php xml download

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

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

PHP脚本正在发送header("Content-type: text/xml"),并且webclient尝试从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);

1 个答案:

答案 0 :(得分:0)

如果您只想下载此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>";