远程服务器返回错误:(404)Not Found - HttpWebResponse

时间:2012-09-28 10:12:41

标签: c# javascript jquery asp.net ajax

我正在使用代理文件来允许我们的系统使用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。

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

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)

那对我来说会建议我的网址构建有问题,但是使用Chrome的网络开发者工具我可以复制传递给代理的确切查询字符串,将其粘贴到浏览器地址栏中,然后访问该网页问题,这意味着建立的网址没有问题。所以我不知道为什么这个人会回复404.如果有人能给我任何建议,我会非常感激。

2 个答案:

答案 0 :(得分:4)

尝试在AJAX代码中使用“GET”代替“POST”

答案 1 :(得分:0)

在Darwish的建议的帮助下,我发现我需要做的是将Web请求更改为GET而不是POST,使我的Proxy文件看起来像这样:

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