ASP.NET equivalent of this PHP code?
We have a flash developer who uses calls a file called proxy.php with the querystring ?url="http://feedburner/whatever" to access external data from rss feeds from domains that not by default accessible from swf code. for example I could have the following in the browser: http://localhost/proxy.php?url=feedburner.com/a_feed and the browser would display the page as if I'd put the feedburner url directly in the browser address bar. The PHP code in this proxy.php file is below.
$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;
}
It works fine but because of hosting limitations we need to replicate the functionality in asp.net. I don't know PHP and despite efforts to understand the code I am failing miserably. I need to be able the duplicate the functionality I described in the first paragraph wi开发者_如何学Cth asp.net but despite googling and trying a technique with XmlTextWriter inside a ashx file I have failed. What am I missing here?
I'm guessing a response.redirect would not work as it tells the source to go to the external domain itself and we want to avoid that.
How would I achieve this PHP code functionality in ASP.NET?
All he is doing is invoking CURL which is an HTTP client (amongst other things) to download the file then streaming it out over the response. You can replicate the functionality by calling HTTPWebRequest. There is a tutorial here:
http://support.microsoft.com/kb/303436
In case anyone would like a code snippet, Based on that link I wrote the code below and it does just the trick (obviously it's hard coded right now but hey...):
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);
}
精彩评论