Skip to main content
 首页 » 编程设计

c#之代理asp不加载外部xml

2024年02月23日20jyk

我正在尝试使用 jquery 和 (aspx) 代理加载远程 rss 提要。我读了很多关于这个问题的问题,但我的问题略有不同。
我有一个包含用户订阅的 XML 文件。对于每个条目,我想加载一些提要,比如前 3 个提要。
我可以正确检索订阅列表,但无法获取提要条目。代理一直给出这个异常:

Riga 22:             HttpWebRequest request = (HttpWebRequest) WebRequest.Create(proxyURL); 
Riga 23:             request.Method = "GET"; 
Riga 24:             HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
Riga 25:  
Riga 26:             if (response.StatusCode.ToString().ToLower() == "ok") 
 
[SocketException (0x274c): Impossibile stabilire la connessione. Risposta non corretta della parte connessa dopo l'intervallo di tempo oppure mancata risposta dall'host collegato 213.92.16.191:80] 
 
 
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +269 
   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +649 
 
[WebException: Impossibile effettuare la connessione al server remoto.] 
   System.Net.HttpWebRequest.GetResponse() +1126 
   Proxy.Page_Load(Object sender, EventArgs e) in c:\Users\andrea\documents\visual_studio_2010\websites\leafhouse\Proxy.aspx.cs:24 
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25 
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42 
   System.Web.UI.Control.OnLoad(EventArgs e) +132 
   System.Web.UI.Control.LoadRecursive() +66 
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428 

这是我的 proxy.aspx 页面:

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net; 
using System.IO; 
 
public partial class Proxy : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        string proxyURL = string.Empty; 
        try 
        { 
            proxyURL = HttpUtility.UrlDecode(Request.QueryString["u"].ToString()); 
 
        if (proxyURL != string.Empty) 
        { 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(proxyURL); 
            request.Method = "GET"; 
            HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
 
            if (response.StatusCode.ToString().ToLower() == "ok") 
            { 
                string contentType = response.ContentType; 
                Stream content = response.GetResponseStream(); 
                StreamReader contentReader = new StreamReader(content); 
                Response.ContentType = contentType; 
                Response.Write(contentReader.ReadToEnd()); 
            } 
        } 
    } 
    catch(Exception ex) 
    { 
        Console.WriteLine(ex.Message); 
    } 
} 

这是我使用的jquery:

$.ajax({ 
    type: "GET", 
    url: "proxy.aspx?u=" + encodeURI("http://" + serverAddress + ":82") + "/RSSReaderSubscriptions.xml", 
    dataType: "xml", 
    success: function (data) { 
        $(data).find('url').each(function (index, element) { 
            if (index < 3) { 
                $.ajax({ 
                    type: "GET", 
                    url: "proxy.aspx?u=" + encodeURI($(this).text()), 
                    dataType: "xml", 
                    success: function (data) { 
                        parseRSS(data); 
                    }, 
                    error: function () { 
                        console.log("error"); 
                }); 
            } 
        }); 
    }, 
    error: function () { 
        console.log("error"); 
}); 

如何让代理加载 rss 提要?
任何帮助是极大的赞赏!谢谢!

请您参考如下方法:

这看起来更像是网络/连接问题,而不是代码中的错误。

您可以“手动”连接到 Feed 的 URL 吗?也许某些代理阻止了对它们的访问...

一些备注:

  • 要完成这些工作,最好使用 HttpHandlers 而不是 aspx 页面以避免完整(和时间/资源消耗)Page Lifecycle .在 Encosia 上有一篇关于此的非常有趣的文章 here .

  • 最好使用 jquery ajax 选项对象的 data 属性来传递数据,而不是像您当前正在做的那样构造查询字符串。

希望这对您有所帮助,d。