/// <summary> /// Send the request to WCF Service /// </summary> /// <param name="template">Object template like: User</param> /// <param name="action">Action like: Delete</param> /// <param name="method">Request method</param> /// <param name="t">Object like: User</param> private void SendRequest(string template, string action, HttpMethod method, T t) { string jsonData = JsonHelp.JsonSerialize <T>(t); if (string.IsNullOrEmpty(jsonData)) { return; } byte[] data = UnicodeEncoding.UTF8.GetBytes(jsonData); httpRequest = HttpWebRequest.CreateHttp(string.Format(basicUrl, template, action)); httpRequest.Method = method.ToString(); httpRequest.ContentType = "application/json"; httpRequest.ContentLength = data.Length; try { using (dataStream = httpRequest.GetRequestStream()) { dataStream.Write(data, 0, data.Length); } } catch (WebException we) { StrMessage = we.Message; } }
/// <summary> /// This method is called 'Ping()' to keep in mind that his responsibility is just to wakup the web service, /// nothing more than that. /// </summary> /// <param name="url"></param> /// <returns></returns> public static LoginResponse Ping(string url) { //Set the request LoginRequestQueue loginRequestQueue = new LoginRequestQueue { Username = Config.Instance.UserAllowedToUseServiceLogin, Password = Config.Instance.UserAllowedToUseServicePassword }; //Get attrubute service var attribute = typeof(LoginRequestQueue).GetCustomAttributes(true).OfType <TokenHandler.Attributes.ServiceRequestAttribute>().FirstOrDefault(); //Call if (attribute == null) { //-------------------------------------------------------------- // Erreur dans la réponse //-------------------------------------------------------------- var resp = Activator.CreateInstance <LoginResponse>(); resp.Code = WindServMessagesConst.AttributeMissingCode; resp.Message = $"{WindServMessagesConst.AttributeMissingMessage} {typeof(LoginRequestQueue).Name}"; resp.ResponseMsg = null; return(resp); } try { #region Timeout management //ServicePointManager.ServerCertificateValidationCallback += (se, certificate, chain, sslPolicyErrors) => true; //httpWebRequest.Timeout = (httpWebRequest.Timeout * 10); //httpWebRequest.Timeout = (120); #endregion System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var httpWebRequest = (HttpWebRequest)WebRequest.Create(url + WindServMessagesConst.HttpWebRequestSeparator + attribute.Url); httpWebRequest.ContentType = WindServMessagesConst.HttpWebRequestContentType; httpWebRequest.Method = WindServMessagesConst.HttpWebRequestMethod; var json = JsonHelp.JsonSerialize(loginRequestQueue); using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write(json); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); //Web server do not respond if (httpResponse.StatusCode != HttpStatusCode.OK) { var resp = Activator.CreateInstance <LoginResponse>(); resp.Code = WindServMessagesConst.ServerDoNotRespondCode; resp.ResponseMsg = $"{WindServMessagesConst .ServerDoNotRespondMessage} {httpResponse.StatusCode.ToString()}"; return(resp); } //Ok wen service answered something var result = Activator.CreateInstance <LoginResponse>(); using (var respStream = httpResponse.GetResponseStream()) { if (respStream == null) { return(null); } var reader = new StreamReader(respStream); var rep = @reader.ReadToEnd().Trim(); try { result = (LoginResponse)JsonHelp.JsonDeserialize <LoginResponse>(rep);//response ok } catch (Exception e) { //---------------------------------- // Erreur dans le message de retour //---------------------------------- var resp = Activator.CreateInstance <LoginResponse>(); resp.Code = WindServMessagesConst.ServerErreurResponseCode; resp.ResponseMsg = e.Message; return(resp); } } return(result); } catch (WebException e) { var resp = Activator.CreateInstance <LoginResponse>(); resp.Code = WindServMessagesConst.WebExceptionCode; resp.ResponseMsg = e.Message; return(resp); } catch (UriFormatException e) { var resp = Activator.CreateInstance <LoginResponse>(); resp.Code = WindServMessagesConst.UriFormatExceptionCode; resp.ResponseMsg = e.Message; return(resp); } catch (Exception e) { //------------------------------- // Erreur générale du Web Service //------------------------------- var resp = Activator.CreateInstance <LoginResponse>(); resp.Code = WindServMessagesConst.GlobalExceptionCode; resp.ResponseMsg = e.Message; return(resp); } }
/// <summary> /// /// </summary> /// <typeparam name="TRequestContractType"></typeparam> /// <typeparam name="TResultType"></typeparam> /// <param name="dRequest"></param> /// <returns></returns> public List <TResultType> CallService <TRequestContractType, TResultType>(List <TRequestContractType> dRequest) where TResultType : ModelBusiness.Response where TRequestContractType : Contracts.BaseContractERP { var attribute = typeof(TRequestContractType).GetCustomAttributes(true).OfType <ServiceRequestAttribute>().FirstOrDefault(); if (attribute == null) { //-------------------------------------------------------------- // Erreur dans la réponse //-------------------------------------------------------------- var resultError = new List <TResultType>(); foreach (var r in dRequest) { var resp = Activator.CreateInstance <TResultType>(); resp.Code = "9005"; resp.Message = "Attribut RequestContractType manquant pour le type " + typeof(TRequestContractType).Name; resp.ErpOprNumber = Guid.Empty; resp.DynamicsOprNumber = Guid.Empty; resultError.Add(resp); } return(resultError); } try { var httpWebRequest = (HttpWebRequest)WebRequest.Create(_clientConfiguration.UriString + "\\" + attribute.Url); //httpWebRequest.Timeout = (httpWebRequest.Timeout * 10); //httpWebRequest.Timeout = (120); httpWebRequest.ContentType = "application/json; charset=utf-8; "; httpWebRequest.Method = "POST"; var json = JsonHelp.JsonSerialize(dRequest); // Envoyer les données au service. using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())){ streamWriter.Write(json); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); if (httpResponse.StatusCode != HttpStatusCode.OK) { //-------------------------------------------------------------- // Erreur dans la réponse //-------------------------------------------------------------- var resultError = new List <TResultType>(); foreach (var r in dRequest) { var resp = Activator.CreateInstance <TResultType>(); resp.Code = "9004"; resp.Message = "Aucune réponse : " + httpResponse.StatusCode.ToString(); resp.ErpOprNumber = Guid.Empty; resp.DynamicsOprNumber = Guid.Empty; resultError.Add(resp); } return(resultError); } List <TResultType> result; using (var respStream = httpResponse.GetResponseStream()) { if (respStream == null) { return(null); } var reader = new StreamReader(respStream); var rep = @reader.ReadToEnd().Trim(); try { result = (List <TResultType>)JsonHelp.JsonDeserialize <List <TResultType> >(rep); } catch (Exception e) { //----------------------------------------------------------------- // Erreur dans le message de retour = Renvoi du message FrameWork //----------------------------------------------------------------- var resultError = new List <TResultType>(); foreach (var r in dRequest) { var resp = Activator.CreateInstance <TResultType>(); resp.Code = "9999"; resp.Message = e.Message; resp.ErpOprNumber = Guid.Empty; resp.DynamicsOprNumber = Guid.Empty; resultError.Add(resp); } return(resultError); } } return(result); } catch (WebException e) { var resultError = new List <TResultType>(); foreach (var r in dRequest) { var resp = Activator.CreateInstance <TResultType>(); resp.Code = "9000"; resp.Message = e.Message; resp.ErpOprNumber = Guid.Empty; resp.DynamicsOprNumber = Guid.Empty; resultError.Add(resp); } return(resultError); } catch (UriFormatException e) { var resultError = new List <TResultType>(); foreach (var r in dRequest) { var resp = Activator.CreateInstance <TResultType>(); resp.Code = "9001"; resp.Message = e.Message; resp.ErpOprNumber = Guid.Empty; resp.DynamicsOprNumber = Guid.Empty; resultError.Add(resp); } return(resultError); } catch (Exception e) { //----------------------------------------------------------------- // Erreur générale du Web Service le message de retour = // Renvoi du message FrameWork //----------------------------------------------------------------- var resultError = new List <TResultType>(); foreach (var r in dRequest) { var resp = Activator.CreateInstance <TResultType>(); resp.Code = "9999"; resp.Message = e.Message; resp.ErpOprNumber = Guid.Empty; resp.DynamicsOprNumber = Guid.Empty; resultError.Add(resp); } return(resultError); } }