2

我正在使用代理文件来允许我们的系统使用 ajax 从我们系统的不同子域加载页面。我第一次尝试成功地做到了这一点,但我的第二次尝试给了我一个错误,我正在努力找出原因,任何帮助将不胜感激。

首先这是我的 Proxy.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    string proxyURL = HttpUtility.UrlDecode(Request.QueryString["u"]);

    if (proxyURL != string.Empty)
    {
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(proxyURL);
        request.Method = "POST";
        request.ContentLength = 0;
        HttpWebResponse response = (HttpWebResponse) request.GetResponse();

        if (response.StatusCode.ToString().ToLower() == "ok")
        {
            string contentType = response.ContentType;
            Stream content = response.GetResponseStream();
            if (content != null)
            {
                StreamReader contentReader = new StreamReader(content);
                Response.ContentType = contentType;
                Response.Write(contentReader.ReadToEnd());
            }
        }
    }
}

我的 HTML/Javascript 就是这样的:

<script>
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "Proxy.aspx?u=<%=GetUrl()%>",
            success: function (data) {
                $('#iFrameHolder').html(data);
            }
        });
    });
</script>

<div id="iFrameHolder"></div>

然后我只使用 GetUrl() 函数来构建我需要从子域上的项目中获取的任何页面的 url。

我用一个 url 完全没有问题,但是第二次尝试我收到了这个错误:

System.Net.WebException: The remote server returned an error: (404) Not Found.     
at System.Net.HttpWebRequest.GetResponse()     
at G2F.Collective.WebApplication.Shared.Proxy.Page_Load(Object sender, EventArgs e) 
in D:\My Documents\Firefly\Collective\Dev\Solution\WebSites\G2F.Collective.WebApplication\Shared\Proxy.aspx.cs:line 26     
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)     
at System.Web.UI.Control.LoadRecursive()     
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

对我来说,这表明我构建的 url 有问题,但使用 Chrome 的 Web 开发人员工具,我可以复制传递给代理的确切查询字符串,将其粘贴到浏览器地址栏中,然后毫无问题地访问该页面,这意味着正在构建的 url 没有问题。所以我不知道为什么这个返回 404。如果有人能给我任何建议,我将不胜感激。

4

2 回答 2

5

尝试在 AJAX 代码中使用“GET”而不是“POST”

于 2012-09-29T03:13:54.173 回答
0

在 Darwish 的建议的帮助下,我发现我需要做的是将 Web 请求更改为 GET 而不是 POST,使我的代理文件如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    string proxyURL = HttpUtility.UrlDecode(Request.QueryString["u"]);

    if (proxyURL != string.Empty)
    {
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(proxyURL);
        request.Method = "GET";
        request.ContentLength = 0;
        HttpWebResponse response = (HttpWebResponse) request.GetResponse();

        if (response.StatusCode.ToString().ToLower() == "ok")
        {
            string contentType = response.ContentType;
            Stream content = response.GetResponseStream();
            if (content != null)
            {
                StreamReader contentReader = new StreamReader(content);
                Response.ContentType = contentType;
                Response.Write(contentReader.ReadToEnd());
            }
        }
    }
}
于 2012-10-01T07:58:58.277 回答