c#HTTP 400错误(错误请求)由HttpWebRequest抛出(intermitent)

时间:2013-03-01 20:32:35

标签: c# httpwebrequest request

我遇到Web服务问题。从Web方法我调用另一个方法来发出HTTP请求。我需要做的是从URL获取XML响应并进行处理。这是我发出HTTP请求的代码:

    private XmlDocument makeHTTPrequest(string url, string parametersStr)
    {
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);

        byte[] buffer = Encoding.ASCII.GetBytes(parametersStr);
        Stream sAnswer = null;
        XmlDocument xDoc = new XmlDocument();
        string proxyAddress = "";
        int proxyPort = 0;
        int requestTimeout = 0;

        try
        {
            proxyAddress = ConfigurationSettings.AppSettings["ProxyAddress"];
            proxyPort = Convert.ToInt32(ConfigurationSettings.AppSettings["ProxyPort"]);
            requestTimeout = Convert.ToInt32(ConfigurationSettings.AppSettings["RequestTimeout"]);

            WebReq.Method = "POST";
            WebReq.ContentType = "application/x-www-form-urlencoded";
            WebReq.ContentLength = buffer.Length;

            if (proxyAddress != "0" && proxyPort != 0)
            {
                WebProxy proxy = null;
                proxy = new WebProxy(proxyAddress, proxyPort);
                proxy.UseDefaultCredentials = true;
                WebReq.Proxy = proxy;
            }

            if (requestTimeout != 0)
            {
                WebReq.Timeout = requestTimeout;
            }

            Stream PostData = WebReq.GetRequestStream();
            PostData.Write(buffer, 0, buffer.Length);
            PostData.Close();

            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            if (WebResp.StatusCode == HttpStatusCode.OK)
            {
                sAnswer = WebResp.GetResponseStream();
                xDoc.Load(sAnswer);
            }
            else
            {
                throw new Exception("No se obtuvo una respuesta satisfactoria del servidor\r\nHTTP Error Code:" + WebResp.StatusCode.ToString());
            }

            if (xDoc == null) throw new Exception("El XmlDocument obtenido de DineroMail es null");

            return xDoc;
        }
        catch (Exception ex)
        {
            string datosStr = "";
            foreach (DictionaryEntry entry in ex.Data)
            {
                datosStr = entry.Key.ToString() + ": " + entry.Value.ToString() + "\r\n";
            }
            this.WriteToEventLog("Metodo makeHTTPrequest(string url, string parametersStr)\r\nParametros: url=" + url + " parametersStr= " + parametersStr + "\r\nDatos Excepcion (Cantidad "+ex.Data.Count.ToString()+"):\r\n"+ datosStr +"Descripcion:\r\n" + ex.Message + "\r\nSource:\r\n" + ex.Source + "\r\nStackTrace:\r\n" + ex.StackTrace + "\r\n");
            throw ex;
        }
    }

这在我的开发环境(我的PC)上运行良好,但是当我转移到Production Server环境(带有IIS的Windows 2008)时,我在创建WebRequest时遇到HTTP 400错误。 为了使调试更加困难,这种情况会间歇性地发生。这意味着它可以运行一段时间,但一段时间后它会停止工作并抛出HTTP 400错误。 另一个额外的信息:从另一个Web服务Web方法我调用另一个Web服务,有时也会间歇性地抛出相同的错误。 另一个额外信息:我通过公司代理访问Internet 有人知道什么是错的吗? 我一直在努力工作几天没有运气,所以欢迎任何想法: - )

谢谢!

1 个答案:

答案 0 :(得分:0)

此处报告了类似的问题:.Net HttpWebRequest.GetResponse() raises exception when http status code 400 (bad request) is returned

此外,我注意到您没有处置Response对象。垃圾收集器的最终化队列中的先前响应是否仍然存在可能导致问题?

using( HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse() )
{
  if (WebResp.StatusCode == HttpStatusCode.OK)
            {
                sAnswer = WebResp.GetResponseStream();
                xDoc.Load(sAnswer);
            }
            else
            {
                throw new Exception("No se obtuvo una respuesta satisfactoria del servidor\r\nHTTP Error Code:" + WebResp.StatusCode.ToString());
            }
}