public ValidationResult Validate() { ValidationResult result = new ValidationResult(); if (string.IsNullOrEmpty(LocalDirectory) || string.IsNullOrEmpty(RemoteURL)) { result.Errors.Add("Required parameter(s) missing"); return(result); } if (!Directory.Exists(LocalDirectory)) { result.Errors.Add("Local Directory invalid"); } if (!RemoteURL.EndsWith("/")) { result.Errors.Add("Remote URL must end with '/'"); } if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) { result.AddWarning("Username or password is missing. FTP access will use anonymous access."); } return(result); }
/// <summary> /// Exceute the given SMSAPI command. /// </summary> /// <param name="Command">The SMSAPI command.</param> /// <param name="Data">The data of the SMSAPI command.</param> /// <param name="Files">Optional files to send.</param> /// <param name="HTTPMethod">The HTTP method to use.</param> public async Task <Stream> Execute(String Command, NameValueCollection Data, Dictionary <String, Stream> Files = null, RequestMethods HTTPMethod = RequestMethods.POST) { #region Init var EventTrackingId = EventTracking_Id.New; var RequestTimeout = TimeSpan.FromSeconds(60); var JSONData = new JObject(); var values = new String[0]; foreach (var key in Data.AllKeys) { values = Data.GetValues(key); if (values.Length == 1) { JSONData[key] = values[0]; } if (values.Length > 1) { JSONData[key] = new JArray(values); } } MemoryStream response = new MemoryStream(); SMSAPIResponseStatus responseStatus = null; #endregion #region Send OnSendSMSAPIRequest event var StartTime = DateTime.UtcNow; try { if (OnSendSMSAPIRequest != null) { await Task.WhenAll(OnSendSMSAPIRequest.GetInvocationList(). Cast <OnSendSMSAPIRequestDelegate>(). Select(e => e(StartTime, this, EventTrackingId, Command, JSONData, RequestTimeout))). ConfigureAwait(false); } } catch (Exception e) { e.Log(nameof(SMSAPIClient) + "." + nameof(OnSendSMSAPIRequest)); } #endregion try { var boundary = "SMSAPI-" + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss") + new Random().Next(int.MinValue, int.MaxValue).ToString() + "-boundary"; var webRequest = WebRequest.Create(RemoteURL.ToString() + Command); webRequest.Method = HTTPMethod.RequestMethodToString(); if (BasicAuthentication != null) { webRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(BasicAuthentication.Username + ":" + BasicAuthentication.Password))); } #region POST | PUT if (RequestMethods.POST.Equals(HTTPMethod) || RequestMethods.PUT.Equals(HTTPMethod)) { Stream stream; if (Files != null && Files.Count > 0) { webRequest.ContentType = "multipart/form-data; boundary=" + boundary; stream = PrepareMultipartContent(boundary, Data, Files); } else { webRequest.ContentType = "application/x-www-form-urlencoded"; stream = PrepareContent(Data); } webRequest.ContentLength = stream.Length; try { stream.Position = 0; CopyStream(stream, webRequest.GetRequestStream()); stream.Close(); } catch (WebException e) { throw new HTTPClientException(e.Message, e); } } #endregion try { CopyStream((await webRequest.GetResponseAsync()).GetResponseStream(), response); } catch (WebException e) { throw new HTTPClientException("Failed to get response from " + webRequest.RequestUri.ToString(), e); } response.Position = 0; responseStatus = ResponseToObject <SMSAPIResponseStatus>(response); } catch (Exception e) { responseStatus = SMSAPIResponseStatus.Failed(e); } #region Send OnSendSMSAPIResponse event var Endtime = DateTime.UtcNow; try { if (OnSendSMSAPIResponse != null) { await Task.WhenAll(OnSendSMSAPIResponse.GetInvocationList(). Cast <OnSendSMSAPIResponseDelegate>(). Select(e => e(Endtime, this, EventTrackingId, Command, JSONData, RequestTimeout, responseStatus, Endtime - StartTime))). ConfigureAwait(false); } } catch (Exception e) { e.Log(nameof(SMSAPIClient) + "." + nameof(OnSendSMSAPIResponse)); } #endregion return(response); }