private void SetCookies(GingerHttpRequestMessage GingerRequestMessage) { switch (GingerRequestMessage.CookieMode) { case GingerHttpRequestMessage.eCookieMode.None: break; case GingerHttpRequestMessage.eCookieMode.New: SessionCokiesDic.Clear(); break; case GingerHttpRequestMessage.eCookieMode.Session: foreach (Cookie cooki in SessionCokiesDic.Values) { Uri domainName = GingerRequestMessage.URL; Cookie ck = new Cookie(); ck.Name = cooki.Name; ck.Value = cooki.Value; if (String.IsNullOrEmpty(cooki.Domain)) { cooki.Domain = domainName.Host; } RequestMessage.Headers.Add(ck.Name, ck.Value); Handler.CookieContainer.Add(cooki); } break; } }
public void HandleRunAction(IPlatformService service, ref NodePlatformAction platformAction) { Platformservice = (IWebServicePlatform)service; RestClient = Platformservice.RestClient; try { GingerHttpRequestMessage Request = GetRequest(platformAction); GingerHttpResponseMessage Response = RestClient.PerformHttpOperation(Request); platformAction.Output.Add("Header: Status Code ", Response.StatusCode.ToString()); foreach (var RespHeader in Response.Headers) { platformAction.Output.Add("Header: " + RespHeader.Key, RespHeader.Value); } platformAction.Output.Add("Request:", Response.RequestBodyString); platformAction.Output.Add("Response:", Response.Resposne); } catch (Exception ex) { platformAction.addError(ex.Message); } }
private void PreparBasicRequest(GingerHttpRequestMessage GingerRequestMessage, GingerHttpResponseMessage GRM) { GRM.RequestBodyString = GingerRequestMessage.BodyString; Client.BaseAddress = GingerRequestMessage.URL; HttpMethod Method = new HttpMethod(GingerRequestMessage.Method.ToUpper()); RequestMessage = new HttpRequestMessage(Method, Client.BaseAddress); SetContentType(GingerRequestMessage); foreach (KeyValuePair <string, string> header in GingerRequestMessage.Headers) { RequestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value); } if (GingerRequestMessage.HttpVersion == ehttpVersion.HTTPV10) { RequestMessage.Version = HttpVersion.Version10; } else if (GingerRequestMessage.HttpVersion == ehttpVersion.HTTPV20) { RequestMessage.Version = HttpVersion.Version20; } else { RequestMessage.Version = HttpVersion.Version11; } SetCookies(GingerRequestMessage); SetRequestContent(Method, GingerRequestMessage, GRM); }
private void HandleResponseCookies(GingerHttpRequestMessage GingerRequestMessage) { if (GingerRequestMessage.CookieMode != eCookieMode.None) { CookieCollection responseCookies = Handler.CookieContainer.GetCookies(Client.BaseAddress); foreach (Cookie RespCookie in responseCookies) { if (SessionCokiesDic.Keys.Contains(RespCookie.Name) == false) { SessionCokiesDic.Add(RespCookie.Name, RespCookie); } else { SessionCokiesDic[RespCookie.Name] = RespCookie; } } } }
public GingerHttpResponseMessage PerformHttpOperation(GingerHttpRequestMessage GingerRequestMessage) { GingerHttpResponseMessage GRM = new GingerHttpResponseMessage(); Client = new HttpClient(Handler); PreparBasicRequest(GingerRequestMessage, GRM); HttpResponseMessage Response = null; try { Response = Client.SendAsync(RequestMessage).Result; } catch (Exception e) { } finally { HandleResponseCookies(GingerRequestMessage); } GRM.StatusCode = Response.StatusCode; GRM.Headers = new Dictionary <string, string>(); //keeping the same pattern as existing Ginger Webserviced Plugin foreach (var Header in Response.Headers) { string headerValues = string.Empty; foreach (string val in Header.Value.ToArray()) { headerValues = val + ","; } headerValues = headerValues.Remove(headerValues.Length - 1); GRM.Headers.Add(Header.Key.ToString(), headerValues); } byte[] data = Response.Content.ReadAsByteArrayAsync().Result; GRM.Resposne = Encoding.Default.GetString(data); return(GRM); }
private void SetContentType(GingerHttpRequestMessage GingerRequestMessage) { string ContentType = string.Empty; Enum.TryParse(GingerRequestMessage.ContentType, out GingerRequestMessage.BodyContentType); switch (GingerRequestMessage.BodyContentType) { case eContentType.JSon: Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); ContentType = "application/json"; break; case eContentType.XwwwFormUrlEncoded: Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); ContentType = "application/x-www-form-urlencoded"; break; case eContentType.FormData: Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data")); ContentType = "multipart/form-data"; //update to correct value break; case eContentType.TextPlain: Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain")); ContentType = "text/plain; charset=utf-8"; break; case eContentType.XML: Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); ContentType = "text/xml"; break; } GingerRequestMessage.ContentType = ContentType; }
private GingerHttpRequestMessage GetRequest(NodePlatformAction platformAction) { GingerHttpRequestMessage Request = new GingerHttpRequestMessage(); Request.URL = new Uri(platformAction.InputParams["EndPointURL"].ToString()); if (platformAction.ActionType == "ActWebAPISoap") { Request.Method = "POST"; Request.ContentType = "XML"; } else { Request.Method = platformAction.InputParams["RequestType"].ToString(); Request.ContentType = platformAction.InputParams.ContainsKey("ContentType") ? platformAction.InputParams["ContentType"].ToString() : ""; if (platformAction.InputParams["RequestKeyValues"] is Newtonsoft.Json.Linq.JArray RObj) { Request.RequestKeyValues = new List <RestAPIKeyBodyValues>(); foreach (Newtonsoft.Json.Linq.JToken Jt in RObj.Children()) { RestAPIKeyBodyValues RKV = new RestAPIKeyBodyValues(); if (Jt["ValueType"].ToString() == "1") { RKV.ValueType = RestAPIKeyBodyValues.eValueType.File; Byte[] FileBytes = (Byte[])Jt["FileBytes"]; string Path = Jt["Value"].ToString(); RKV.Filename = System.IO.Path.GetFileName(Path); ByteArrayContent fileContent = new ByteArrayContent(FileBytes); fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); RKV.Content = fileContent; } RKV.Value = Jt["Value"].ToString(); RKV.Param = Jt["Param"].ToString(); Request.RequestKeyValues.Add(RKV); } } } Request.BodyString = platformAction.InputParams.ContainsKey("RequestBody") ? platformAction.InputParams["RequestBody"].ToString() : ""; if (platformAction.InputParams["Headers"] is Newtonsoft.Json.Linq.JObject JsonObj) { foreach (Newtonsoft.Json.Linq.JProperty Jt in JsonObj.Children()) { Request.Headers.Add(new KeyValuePair <string, string>(Jt.Name, Jt.Value.ToString())); } } return(Request); }
private void SetRequestContent(HttpMethod RequestMethod, GingerHttpRequestMessage GingerRequestMessage, GingerHttpResponseMessage GRM) { List <KeyValuePair <string, string> > KeyValues = new List <KeyValuePair <string, string> >(); if (RequestMethod.Method == HttpMethod.Get.Method) { if (GingerRequestMessage.BodyContentType == eContentType.XwwwFormUrlEncoded) { string GetRequest = "?"; if (GingerRequestMessage.RequestKeyValues.Count > 0) { for (int i = 0; i < GingerRequestMessage.RequestKeyValues.Count; i++) { GetRequest += GingerRequestMessage.RequestKeyValues[i].Param + "=" + GingerRequestMessage.RequestKeyValues[i].Value + "&"; } } string ValuesURL = GingerRequestMessage.URL.ToString() + GetRequest.Substring(0, GetRequest.Length - 1); Client.BaseAddress = new Uri(ValuesURL); } else { Client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", GingerRequestMessage.ContentType); } } else { if ((GingerRequestMessage.BodyContentType != eContentType.XwwwFormUrlEncoded) && (GingerRequestMessage.BodyContentType != eContentType.FormData)) { BodyString = GingerRequestMessage.BodyString; } switch (GingerRequestMessage.BodyContentType) { case eContentType.XwwwFormUrlEncoded: if (GingerRequestMessage.RequestKeyValues.Count > 0) { List <KeyValuePair <string, string> > RequestKeyValues = new List <KeyValuePair <string, string> >(); for (int i = 0; i < GingerRequestMessage.RequestKeyValues.Count; i++) { KeyValues.Add(new KeyValuePair <string, string>(GingerRequestMessage.RequestKeyValues[i].Param, GingerRequestMessage.RequestKeyValues[i].Value)); } RequestMessage.Content = new FormUrlEncodedContent(RequestKeyValues); GRM.RequestBodyString = RequestMessage.Content.ToString(); } break; case eContentType.FormData: if (GingerRequestMessage.RequestKeyValues.Count > 0) { MultipartFormDataContent requestContent = new MultipartFormDataContent(); List <KeyValuePair <string, string> > FormDataKeyValues = new List <KeyValuePair <string, string> >(); for (int i = 0; i < GingerRequestMessage.RequestKeyValues.Count; i++) { if (GingerRequestMessage.RequestKeyValues[i].ValueType == eValueType.Text) { FormDataKeyValues.Add(new KeyValuePair <string, string>(GingerRequestMessage.RequestKeyValues[i].Param, GingerRequestMessage.RequestKeyValues[i].Value)); requestContent.Add(new StringContent(GingerRequestMessage.RequestKeyValues[i].Value), GingerRequestMessage.RequestKeyValues[i].Param); } if (GingerRequestMessage.RequestKeyValues[i].ValueType == eValueType.File) { requestContent.Add(GingerRequestMessage.RequestKeyValues[i].Content, GingerRequestMessage.RequestKeyValues[i].Param, GingerRequestMessage.RequestKeyValues[i].Filename); } } GRM.RequestBodyString = requestContent.ToString(); RequestMessage.Content = requestContent; } break; case eContentType.XML: string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); if (BodyString.StartsWith(_byteOrderMarkUtf8)) { var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length - 1; BodyString = BodyString.Remove(0, lastIndexOfUtf8); } RequestMessage.Content = new StringContent(BodyString, Encoding.UTF8, GingerRequestMessage.ContentType); break; default: RequestMessage.Content = new StringContent(BodyString, Encoding.UTF8, GingerRequestMessage.ContentType); break; } } }