我有一个关于服务器响应的问题。(返回 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;
}
}