我有一个关于服务器响应的问题。(返回403禁止)。
当我在浏览器中检查相同的url时,我可以看到该页面。
我已经写了下面的代码来获得响应。
相同的代码我在我的本地PC上执行,它工作得很好。
HttpWebRequest request = WebRequest.Create(RequestUrl) as HttpWebRequest;
request.Method = "POST";
request.CookieContainer = new CookieContainer();
request.Timeout = 600000;
request.Accept = "*/*";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0";
WebResponse response;
try
{
response = request.GetResponse();
string StatusDescription = ((HttpWebResponse)response).StatusDescription;
if (StatusDescription.ToLower() == "ok")
{
Stream dataStream = response.GetResponseStream();
//XmlDocument XmlResponse = new XmlDocument();
//XmlResponse.Load(dataStream);
//XmlResponse.Save(Server.MapPath("~/XMLResponse.xml"));
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
responseFromServer = responseFromServer.Replace(" ", "");
responseFromServer = responseFromServer.Replace("&", "&");
reader.Close();
dataStream.Dispose();
response.Close();
responseFromServer = GetStringsBetweenTags(responseFromServer, "<head>", "</head>");
return responseFromServer;
}
else
{
//To Do : add code for notify error via email
}
}
catch (Exception ex)
{
WebClient webclient = new WebClient();
using (StreamReader reader = new StreamReader(webclient.OpenRead(RequestUrl)))
{
string responseFromServer = reader.ReadToEnd();
responseFromServer = responseFromServer.Replace(" ", "");
responseFromServer = responseFromServer.Replace("&", "&");
reader.Close();
webclient.Dispose();
responseFromServer = GetStringsBetweenTags(responseFromServer, "<head>", "</head>");
return responseFromServer;
}
}
下载提琴手(http://www.fiddler2.com/fiddler2/)
然后导航到URL并查看标题。然后查看应用程序正在发送的标头。您要 post 的URL显然阻止了某些请求。