C# 远程服务器返回错误:(404) Not Found - HttpWebResponse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12638013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The remote server returned an error: (404) Not Found - HttpWebResponse
提问by VampiricMonkey
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.
我正在使用代理文件来允许我们的系统使用 ajax 从我们系统的不同子域加载页面。我在第一次尝试时成功地做到了这一点,但我的第二次尝试给了我一个错误,我正在努力找出原因,任何帮助将不胜感激。
Firstly this is my Proxy.aspx.cs:
首先这是我的 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:
我的 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>
Then I just use the GetUrl() function to build the url of whatever page I require from the project on the subdomain.
然后我只使用 GetUrl() 函数来构建我需要从子域上的项目中获取的任何页面的 url。
I got this working no problem at all with one url, but for the second attempt I received this error:
我用一个 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)
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.
对我来说,这表明我构建的 url 有问题,但是使用 Chrome 的 Web 开发人员工具,我可以复制传递给代理的确切查询字符串,将其粘贴到浏览器地址栏中,然后访问该页面而没有任何问题,这意味着正在构建的 url 没有问题。所以我不知道为什么这个返回 404。如果有人能给我任何建议,我将不胜感激。
采纳答案by VampiricMonkey
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:
在 Darwish 的建议的帮助下,我发现我需要做的是将 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());
}
}
}
}
回答by Darwish
Try to use "GET" instead of "POST" in your AJAX code
尝试在 AJAX 代码中使用“GET”而不是“POST”

