2010-02-22 79 views
3

我们有一个flash开发人员使用调用一个名为proxy.php的文件与querystring?url =“http:// feedburner/whatever”来访问来自不是默认从swf代码访问的域的rss提要的外部数据。例如,我可以在浏览器中使用以下内容:http://localhost/proxy.php?url=feedburner.com/a_feed并且浏览器将显示页面,就好像我将feedburner网址直接放在浏览器地址栏中一样。这个proxy.php文件中的PHP代码如下。ASP.NET等价于此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描述的功能,但是尽管使用Google搜索器中的XmlTextWriter使用Google搜索技术,但我失败了。我在这里错过了什么?

我猜response.redirect不会工作,因为它告诉源去外部域本身,我们想要避免这一点。

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

+0

看看这里:http://www.php.net/manual/en/ref.curl.php – 2010-02-22 19:19:19

+0

通用一个,任何语言将被调用命令行卷曲。 – takeshin 2010-11-17 22:18:07

回答

2

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

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

+0

真棒链接刺激,谢谢。多好的测试应用程序!我将发布相当于下面的php的代码。 – 2010-02-22 20:08:07

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); 
}