3

我们有一个 Flash 开发人员,他使用查询字符串 ?url="http://feedburner/whatever" 调用名为 proxy.php 的文件来访问来自域的 rss 提要的外部数据,这些域默认情况下不能从 swf 代码访问。例如,我可以在浏览器中有以下内容:http://localhost/proxy.php?url=feedburner.com/a_feed并且浏览器会显示页面,就好像我将 feedburner url 直接放在浏览器地址栏中一样. 此 proxy.php 文件中的 PHP 代码如下。

$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($post_data);

$ch = curl_init( $_GET['url'] ); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

if ( strlen($post_data)>0 ){
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}

$response = curl_exec($ch);     

if (curl_errno($ch)) {
    print curl_error($ch);
} else {
    curl_close($ch);
    //$response=split("iso-8859-2",$response);
    //$response=join("UTF-8",$response);
    print $response;
}

它工作正常,但由于托管限制,我们需要在 asp.net 中复制功能。我不了解 PHP,尽管努力理解代码,但我还是失败了。我需要能够使用 asp.net 复制我在第一段中描述的功能,但是尽管谷歌搜索并尝试在 ashx 文件中使用 XmlTextWriter 的技术我失败了。我在这里想念什么?

我猜 response.redirect 不起作用,因为它告诉源转到外部域本身,我们希望避免这种情况。

我将如何在 ASP.NET 中实现这个 PHP 代码功能?

4

2 回答 2

2

他所做的只是调用 CURL,它是一个 HTTP 客户端(除其他外)来下载文件,然后通过响应将其流式传输。您可以通过调用 HTTPWebRequest 来复制该功能。这里有一个教程:

http://support.microsoft.com/kb/303436

于 2010-02-22T19:22:35.410 回答
1

万一有人想要一个代码片段,基于该链接,我编写了下面的代码,它只是把戏(显然它现在是硬编码的,但是嘿......):

protected void Page_Load(object sender, EventArgs e)
{
    string URL = "http://feeds2.feedburner.com/the-foreigner";
    HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(URL);
    HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();

    //Read the raw HTML from the request
    StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), Encoding.ASCII);
    //Convert the stream to a string
    string s = sr.ReadToEnd();
    sr.Close();
    Response.Write(s); 
}
于 2010-02-22T20:08:48.360 回答