The remote server returned an error: (404) Not Found - HttpWebResponse

72,926

Solution 1

With the help of the suggestion by Darwish I discovered what I needed to do was change the web request to GET instead of POST, making my Proxy file look like this:

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

Solution 2

Try to use "GET" instead of "POST" in your AJAX code

Share:
72,926
Claire_Monkey
Author by

Claire_Monkey

I have been developing web sites since the age of 12, and been a professional in the industry for over 15 years. I work in both ASP.Net MVC and Classic ASP (with the occasional dip into PHP) using both MS SQL and MySQL, with a lot of javascript thrown in. I am slightly obsessed with the colour purple and an avid follower of the MCU.

Updated on July 26, 2020

Comments

  • Claire_Monkey
    Claire_Monkey almost 4 years

    I am using using a Proxy file to allow our system to use ajax to load in pages from an a different subdomain of our system. I successfully did this with my first attempt, but my second attempt is giving me an error, and I'm struggling to work out why, any help would be appreciated.

    Firstly this is my 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());
                }
            }
        }
    }
    

    My HTML/Javascript is just this:

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

    Then I just use the GetUrl() function to build the url of whatever page I require from the project on the subdomain.

    I got this working no problem at all with one url, but for the second attempt I received this error:

    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)
    

    That to me would suggest something was wrong with my url being built, but using Chrome's web developer tools I can copy out the exact querystring being passed to the proxy, paste it into the browser address bar, and visit the page without any issue at all, which means there is no issue with the url being built. So I have no idea why this one returns a 404. If anyone can give me any suggestions, I'd greatly appreciate it.

  • psubsee2003
    psubsee2003 over 11 years
    This should be a comment not an answer. A solid answer should directly answer the question and contain some explanation of why or how it resolves the OP's problem. A comment is better used to make suggestions and to get more information about what the OP has tried.
  • Claire_Monkey
    Claire_Monkey over 11 years
    Was almost the right answer! The ajax post/get makes no difference, as what actually powers it is the Proxy webrequest. I changed that to GET instead of POST and we have success! No idea why its so different to my other page, but I got it working. Thank you!