Exemplo n.º 1
0
 /// <summary>
 /// Past Data by Http Delete
 /// </summary>
 /// <param name="baseUri"></param>
 /// <param name="apiUrl"></param>
 /// <param name="hearder"></param>
 /// <returns></returns>
 public static async Task <HttpResponseMessage> Delete(Uri baseUri, string apiUrl, RequestHeader hearder)
 {
     using (var client = new HttpClient(new HttpClientHandler {
         UseCookies = false
     })
     {
         Timeout = HelperSettings.ApiTimeout
     })
     {
         // *** Create Cookies Ref:http://stackoverflow.com/questions/12373738/how-do-i-set-a-cookie-on-httpclients-httprequestmessage ***
         client.BaseAddress = baseUri;
         var message = new HttpRequestMessage(HttpMethod.Delete, apiUrl);
         System.Net.ServicePointManager.SecurityProtocol         = SecurityProtocolType.Tls12;
         ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate
                                                                                                           { return(true); });
         object value;
         string key;
         foreach (var prop in hearder.GetType().GetProperties())
         {
             if ((value = prop.GetValue(hearder)) == null)
             {
                 continue;
             }
             key = prop.GetCustomAttribute <HearderAttribute>().Key;
             message.Headers.Add(key, value.ToString());
         }
         //return client.SendAsync(message).Result;
         return(await client.SendAsync(message).ConfigureAwait(false));
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Get/Past Data by Http Post
 /// </summary>
 /// <param name="baseUri">Base URI</param>
 /// <param name="apiUrl">Api URL</param>
 /// <param name="hearder">Cookie Header Content</param>
 /// <param name="content">Response Content</param>
 /// <returns></returns>
 public static async Task <HttpResponseMessage> Post(Uri baseUri, string apiUrl, RequestHeader hearder, HttpContent content, bool contentType = true /* string contentType = "application/json"*/)
 {
     using (var client = new HttpClient(new HttpClientHandler {
     })
     {
         Timeout = HelperSettings.ApiTimeout
     })
     {
         client.BaseAddress = baseUri;
         client.DefaultRequestHeaders.Accept.Clear();
         if (contentType)
         {
             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         }
         //if (!string.IsNullOrEmpty(contentType))
         //{
         //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
         //}
         //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
         //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
         object value;
         string key;
         foreach (var prop in hearder.GetType().GetProperties())
         {
             if ((value = prop.GetValue(hearder)) == null)
             {
                 continue;
             }
             key = prop.GetCustomAttribute <HearderAttribute>().Key;
             client.DefaultRequestHeaders.Add(key, value.ToString());
         }
         //var result = client.PostAsync(apiUrl, content).Result;
         return(await client.PostAsync(apiUrl, content).ConfigureAwait(false));
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Get/Past Data by Http Post
 /// </summary>
 /// <param name="baseUri">Base URI</param>
 /// <param name="apiUrl">Api URL</param>
 /// <param name="hearder">Cookie Header Content</param>
 /// <param name="content">Response Content</param>
 /// <returns></returns>
 public static async Task <HttpResponseMessage> PutAsJson <T>(Uri baseUri, string apiUrl, RequestHeader hearder, T data)
 {
     using (var client = new HttpClient(new HttpClientHandler {
     })
     {
         Timeout = HelperSettings.ApiTimeout
     })
     {
         client.BaseAddress = baseUri;
         client.DefaultRequestHeaders.Accept.Clear();
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         System.Net.ServicePointManager.SecurityProtocol         = SecurityProtocolType.Tls12;
         ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate
                                                                                                           { return(true); });
         object value;
         string key;
         foreach (var prop in hearder.GetType().GetProperties())
         {
             if ((value = prop.GetValue(hearder)) == null)
             {
                 continue;
             }
             key = prop.GetCustomAttribute <HearderAttribute>().Key;
             client.DefaultRequestHeaders.Add(key, value.ToString());
         }
         //var result = client.PutAsJsonAsync(apiUrl, data).Result;
         return(await client.PutAsJsonAsync(apiUrl, data));
     }
 }
        /// <summary>
        /// Fix the request header namespace in outgoing Soap Requests, so that
        /// cross namespace requests can work properly.
        /// </summary>
        /// <param name="signature">The service creation parameters.</param>
        /// <param name="service">The service object for which RequestHeader
        /// needs to be patched.</param>
        private static void FixRequestHeaderNameSpace(AdWordsServiceSignature signature,
                                                      AdsClient service)
        {
            // Set the header namespace prefix. For all /cm services, the members
            // shouldn't have xmlns. For all other services, the members should have
            // /cm as xmlns.
            object[] attributes = service.GetType().GetCustomAttributes(false);
            foreach (object attribute in attributes)
            {
                if (attribute is WebServiceBindingAttribute)
                {
                    WebServiceBindingAttribute binding = (WebServiceBindingAttribute)attribute;
                    string delimiter = "/api/adwords/";
                    string xmlns     = String.Join("", new String[] {
                        binding.Namespace.Substring(0, binding.Namespace.IndexOf(delimiter) +
                                                    delimiter.Length), "cm/", signature.Version
                    });
                    if (xmlns == binding.Namespace)
                    {
                        xmlns = "";
                    }

                    RequestHeader svcRequestHeader = null;
                    PropertyInfo  propInfo         = service.GetType().GetProperty("RequestHeader");
                    if (propInfo != null)
                    {
                        svcRequestHeader = (RequestHeader)propInfo.GetValue(service, null);

                        if (svcRequestHeader != null)
                        {
                            PropertyInfo wsPropInfo = svcRequestHeader.GetType().GetProperty("TargetNamespace");
                            if (wsPropInfo != null)
                            {
                                wsPropInfo.SetValue(svcRequestHeader, xmlns, null);
                            }
                        }
                    }
                }
            }
        }