2

我有一个关于服务器响应的问题。(返回 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("&nbsp;", "");
                        responseFromServer = responseFromServer.Replace("&", "&amp;");
                        reader.Close();
                        webclient.Dispose();
                        responseFromServer = GetStringsBetweenTags(responseFromServer, "<head>", "</head>");
                        return responseFromServer;

                    }

                }
4

1 回答 1

4

下载提琴手 (http://www.fiddler2.com/fiddler2/)

然后导航到 URL 并查看标题。然后查看您的应用程序发送的标头。您发布到的 URL 显然阻止了某些请求。

于 2013-01-17T10:14:36.440 回答