间歇(417)预期失败

时间:2019-08-22 01:47:24

标签: c# .net

大家好,感谢您的提前帮助。我正在开发一个应用程序,该应用程序可以检索csv文件并将其上传到使用HTTP 1.1的IOT设备。我有两个例行公事。第一个例程从设备中删除旧数据:

        public string DeleteQueue()
    {


        Uri myUri = new Uri(url);
        WebRequest myWebRequest = HttpWebRequest.Create(myUri);

        HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
        // https://stackoverflow.com/questions/716436/is-there-a-correct-way-to-dispose-of-a-httpwebrequest
        // This disposes of the WebRequest upon completion
        myHttpWebRequest.KeepAlive = false;
        // https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepoint.maxidletime?view=netframework-4.8
        myHttpWebRequest.ServicePoint.MaxIdleTime = 1;

        NetworkCredential myNetworkCredential = new NetworkCredential(UserName, Password);

        CredentialCache myCredentialCache = new CredentialCache();
        myCredentialCache.Add(myUri, "Digest", myNetworkCredential);

        myHttpWebRequest.PreAuthenticate = true;
        myHttpWebRequest.Credentials = myCredentialCache;

        myHttpWebRequest.Method = "DELETE";
        myHttpWebRequest.ContentLength = 0;

        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
      int StatusCode = 0;
        using (HttpWebResponse myWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse())
        {
            StatusCode = (int)myWebResponse.StatusCode;

            Stream responseStream = myWebResponse.GetResponseStream();

            StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);

            string pageContent = myStreamReader.ReadToEnd();

            responseStream.Close();

            myWebResponse.Close();

        }                

        return StatusCode.ToString();

    }

此例程每次正确完成。第二个例程将数据上传到设备:

        public string UploadPatents(List<Patients> PatientList)
    {
        string resp = "";

        try
        {
            string PatientsToAdd = "";
            foreach (var pt in PatientList)
            {
                PatientsToAdd = PatientsToAdd + pt.PatientID + "," + pt.PatientFirstName + "," + pt.PatientLastName + "," + pt.PatientDOB.ToString("yyyy-MM-dd") + "," + pt.Gender + ",None" + Environment.NewLine; ;
            }

            int contentLength = PatientsToAdd.Length;
            contentLength = System.Text.ASCIIEncoding.Unicode.GetByteCount(PatientsToAdd);

            WebClient client = new WebClient();
            client.Headers[HttpRequestHeader.ContentType] = "application/csv, text/plain";

            client.Credentials = new NetworkCredential(UserName, Password);
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
          System.Net.ServicePointManager.Expect100Continue = false;

            var result = client.UploadString(PatientDataUri, PatientsToAdd);

            client.Dispose();

            resp = "OK";

        }
        catch (WebException webex)
        {
            if (webex.Response != null)
            {

                resp = webex.ToString();
            }
        }

        return resp;
    }

第二个例程有时返回System.Net.WebException:远程服务器返回错误:(417)期望失败,但并非总是如此。同样,如果第一个例程运行并且发生延迟(可能是两分钟),则第二个例程将正确运行。我不确定第一个例程是否使连接保持打开状态或其他问题。任何帮助将不胜感激。

enter code here

0 个答案:

没有答案