动态响应'sometimes'崩溃

本文关键字:崩溃 sometimes 响应 动态 | 更新日期: 2023-09-27 18:12:20

我有一个foreach"If",当条件为真时,我做一个WebResponse在服务器上发布我的项目。

有时代码可以运行两个或多个项目,但有时会崩溃,并出现以下错误:

远程服务器返回错误:(407)Proxy Authentication Required.

代码:

WebClient client = new WebClient();
                string authInfo = "admin:geoserver";
                string address = "http://xxxxxxxx:8080/geoserver/rest/workspaces/";
                client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));

                WebRequest request = WebRequest.Create(address);
                request.ContentType = "text/xml";
                request.Method = "POST";
                request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
                byte[] bret = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>" + nameWS + "</name></workspace>");

                Stream reqstr = request.GetRequestStream();
                reqstr.Write(bret, 0, bret.Length);
                reqstr.Close();

                try
                {
                    WebResponse response = request.GetResponse();
                    response.Close();
                }

My Environment is c# Visual Studio 2010

动态响应'sometimes'崩溃

你多久调用一次这个?正如其他人所建议的那样,这可能是服务器受到DOS保护,并且您的请求被视为那样。例如,使用using块立即处理所有一次性对象也很有价值。我们曾经有过一些问题,因为在我们的网络内部,我们的web服务器打开了太多的连接。你可以把你的代码调整成这样:

using(var client = new WebClient())
{
    string authInfo = "admin:geoserver";
    string address = "http://xxxxxxxx:8080/geoserver/rest/workspaces/";
    client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
    var request = WebRequest.Create(address);
    request.ContentType = "text/xml";
    request.Method = "POST";
    request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
    byte[] bret = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>" + nameWS + "</name></workspace>");
    using (var reqstr = request.GetRequestStream())
    {
        reqstr.Write(bret, 0, bret.Length);
    }
    try
    {
        using (var response = request.GetResponse())
        {
            // your code here...
        }
    }
    catch (Exception exc)
    {
        System.Diagnostics.Debug.WriteLine(exc.Message);
    }
}