2012-09-28 133 views
2

我正在使用代理文件来允许我们的系统使用ajax从我们系统的不同子域中加载页面。我第一次尝试就成功完成了这个任务,但是我的第二次尝试却给了我一个错误,而且我正在努力解决原因,希望能得到任何帮助。远程服务器返回错误:(404)未找到 - HttpWebResponse

首先这是我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()函数来建立的网址无论我在子域上的项目需要什么页面。

我得到这个在所有的工作没有问题,一个网址,但第二次尝试,我收到此错误:

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的Web开发人员工具我可以复制传递给代理的确切查询字符串,将其粘贴到浏览器地址栏中,然后访问该页面而不会有任何问题,这意味着正在构建的网址没有问题。所以我不知道为什么这个返回404。如果有人可以给我任何建议,我将不胜感激。

+2

尽量不要从Chrome浏览器开发工具复制,但要保持断点.NET代码,看看你得到并传递到Web请求的URL,希望这将有助于 –

+0

也尝试过,它构建的网址肯定可以在浏览器中使用,但它仍然是404的,当它碰到GetResponse() – VampiricMonkey

回答

0

“GET”,而不是“POST”与达尔维什的建议的帮助下,我发现了什么,我需要做的是改变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()); 
      } 
     } 
    } 
} 
3

尝试使用您的AJAX代码

+2

这应该是一个commen没有答案。一个可靠的答案应直接回答这个问题,并包含一些解释为什么或如何解决OP问题的解释。评论更好地用于提出建议并获得有关OP已经尝试的更多信息。 – psubsee2003

+0

几乎是正确的答案! ajax post/get没有什么区别,实际上它的功能是代理webrequest。我改变了GET而不是POST,并且我们已经成功了!不知道为什么它与我的其他页面如此不同,但我知道它的工作原理。谢谢! – VampiricMonkey

相关问题