【发布时间】: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