【问题标题】:The remote server returned an error: (404) Not Found - HttpWebResponse远程服务器返回错误:(404) Not Found - HttpWebResponse
【发布时间】:2012-09-20 05:17:54
【问题描述】:

我正在使用代理文件来允许我们的系统使用 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。如果有人能给我任何建议,我将不胜感激。

【问题讨论】:

  • 尽量不要从 chrome 开发工具中复制,而是在您的 .net 代码中保留一个断点,以查看您获取并传递给 Web 请求的 url,希望对您有所帮助
  • 也试过了,它构建的 url 在浏览器中肯定可以工作,但是当它点击 GetResponse() 时仍然是 404

标签: c# javascript jquery asp.net ajax


【解决方案1】:

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

【讨论】:

  • 这应该是评论而不是答案。一个可靠的答案应该直接回答这个问题,并包含一些解释为什么或如何解决 OP 的问题。评论最好用于提出建议并获取有关 OP 尝试过的更多信息。
  • 几乎是正确的答案! ajax post/get 没有区别,因为它实际上是代理网络请求。我将其更改为 GET 而不是 POST,我们成功了!不知道为什么它与我的其他页面如此不同,但我得到了它的工作。谢谢!
【解决方案2】:

在 Darwish 的建议的帮助下,我发现我需要做的是将网络请求更改为 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());
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2014-01-19
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多