コード例 #1
0
        public void PostData(DpfData data, PostSettings ps, string failedDataDir)
        {
            if (data == null || ps == null)
            {
                throw new ArgumentNullException();
            }

            //splitsen in delen
            byte[][] dataParts = data.Split(ps.MaxPostSize);

            for (int i = 0; i < dataParts.Length; i++)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                byte[] dp       = dataParts[i];
                string dpLength = (dp != null) ? dp.Length.ToString() : "-";

                JsonRespons respons = Post(ps.URL, dp, ps.Username, ps.Password);

                sw.Stop();

                if (respons == null)
                {
                    //fout
                    //opslaan data
                    string fileName = string.Format("{2}\\{1} DPF Post Part {0} Failed.csv", i, DateTime.Now.ToString("yyyy-MM-dd HHmmss"), failedDataDir);
                    try
                    {
                        File.WriteAllBytes(fileName, dp);
                        debugLog.AppendFormat("POST Part {0} failed after {1} s.: {2} bytes saved to {3}\r\n", i, sw.Elapsed.TotalSeconds.ToString("F2"), dpLength, fileName);
                    }
                    catch (Exception ex)
                    {
                        debugLog.AppendFormat("POST Part {0} failed after {1} s.: {2} bytes could not be saved to {3}: {4}\r\n", i, sw.Elapsed.TotalSeconds.ToString("F2"), dpLength, fileName, ex.Message);
                    }
                }
                else
                {
                    //succes
                    debugLog.AppendFormat("POST Part {0} succes in {1} s.: {2} bytes, FietsViewerRespons.processId={3}\r\n", i, sw.Elapsed.TotalSeconds.ToString("F2"), dpLength, respons.FietsViewerRespons.ProcessId);
                }
            }
        }
コード例 #2
0
        private JsonRespons Post(string URL, byte[] data, string username = "", string password = "")
        {
            //true=ok, false=fout opgetreden en al een foutmelding doorgegeven
            if (URL == null)
            {
                debugLog.AppendLine("Failed: URL is NULL");
                error = true;
                return(null);
            }
            if (data == null)
            {
                debugLog.AppendLine("Failed: data is NULL");
                error = true;
                return(null);
            }

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                request.KeepAlive       = false;
                request.ProtocolVersion = HttpVersion.Version11;
                request.Timeout         = 5000; //standaard is 100 seconden timeout

                if (username != "")
                {
                    request.Credentials = new NetworkCredential(username, password);
                }
                //request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));

                request.Method        = "POST";
                request.ContentType   = "application/x-www-form-urlencoded"; //"application/json"
                request.ContentLength = data.Length;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                HttpWebResponse response     = (HttpWebResponse)request.GetResponse();
                int             responseCode = (int)response.StatusCode;
                Stream          dataStream   = response.GetResponseStream();
                StreamReader    reader       = new StreamReader(dataStream);
                string          responsData  = reader.ReadToEnd(); // Read the content
                reader.Close();
                response.Close();

                JsonRespons res = null;
                try
                {
                    res = DeserializeFromString <JsonRespons>(responsData);
                }
                catch
                {
                    debugLog.AppendLine(string.Format("Failed: URL {0} Response kon niet geinterpreteerd worden: {1}", URL, responsData));
                    return(null);
                }

                if (responseCode >= 200 && responseCode < 300)
                {
                    return(res);
                }
                else
                {
                    //In de praktijk komt hij hier niet, aangezien errors in de exception belanden.
                    //debugLog.AppendLine(string.Format("Failed: URL {0} Response {1}:{2}", URL, responseCode, response.StatusDescription));
                    debugLog.AppendLine(string.Format("Failed: URL {0} Response: {1}", URL, res.FietsViewerRespons.ToString()));
                    return(null);
                }
            }
            catch (Exception ex)
            {
                if (ex is WebException)
                {
                    debugLog.AppendLine(string.Format("Failed: URL {0} Response: {1}", URL, ex.Message));
                }
                else
                {
                    debugLog.AppendLine(string.Format("ERROR: URL {0} Exception: {1}", URL, ex.Message));
                }
                return(null);
            }
        }