public ServiceResult Post(ContactModel contact, PostSettings postSettings, ILogger logger = null) { var result = new ServiceResult { ServiceResultType = ServiceResultType.None }; var client = new HttpClient(); HttpContent content; try { if (postSettings.EncType == PostEncType.Form) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("ContactName", contact.ContactName), new KeyValuePair <string, string>("Email", contact.Email), new KeyValuePair <string, string>("Phone", contact.Phone), new KeyValuePair <string, string>("Category", contact.Category), new KeyValuePair <string, string>("Subject", contact.Subject), new KeyValuePair <string, string>("Message", contact.Message) }); } else { //string tmpContent = JsonSerializer.Serialize(contact); string tmpContent = JsonConvert.SerializeObject(contact); content = new StringContent(tmpContent, Encoding.UTF8, "application/json"); if (logger != null) { logger.LogInformation(tmpContent); } } var response = client.PostAsync(postSettings.PostURL, content).GetAwaiter().GetResult(); //var contents = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); result.ServiceResultType = ServiceResultType.Success; result.Message = $"Posted to {postSettings.PostURL} - Status code: {response.StatusCode}"; } catch (Exception ex) { result.ServiceResultType = ServiceResultType.Error; result.Message = ex.Message; if (logger != null) { logger.LogInformation(ex, "PostService error"); } } return(result); }
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); } } }
public IActionResult Index(SettingsModel settings) { PostSettings.PostModel(settings); return(Index()); }