Ejemplo n.º 1
0
        public async Task <SailsResponse> PostAsync(string url, Dictionary <string, string> forms = null, string referer = null, bool loging = false)
        {
            SailsResponse httpResp = new SailsResponse();

            httpResp.ResponseUri = url;
            FormUrlEncodedContent content = null;

            if (forms != null && forms.Count > 0)
            {
                content = new FormUrlEncodedContent(forms);
            }
            if (referer != null)
            {
                httpClient.DefaultRequestHeaders.Referrer = new Uri(referer);
            }
            var httpRsp = await httpClient.PostAsync(url, content).ConfigureAwait(false);

            if (httpRsp != null)
            {
                if (loging)
                {
                    cookies.Add(handler.CookieContainer.GetCookies(new Uri(url)));
                }
                httpResp.ResponseUri = httpRsp.RequestMessage.RequestUri.ToString();
                httpResp.Content     = httpRsp.Content.ReadAsStringAsync().Result;
                httpResp.StatusCode  = httpRsp.StatusCode;
            }
            return(httpResp);
        }
Ejemplo n.º 2
0
        public async Task <SailsResponse> PostJAsync(string url, string jsonParams = null, string referer = null, bool loging = false)
        {
            SailsResponse httpResp = new SailsResponse();

            httpResp.ResponseUri = url;
            HttpContent content = null;

            if (!string.IsNullOrWhiteSpace(jsonParams))
            {
                content = new StringContent(jsonParams);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }
            if (referer != null)
            {
                httpClient.DefaultRequestHeaders.Referrer = new Uri(referer);
            }
            var httpRsp = await httpClient.PostAsync(url, content).ConfigureAwait(false);

            if (httpRsp != null)
            {
                if (loging)
                {
                    cookies.Add(handler.CookieContainer.GetCookies(new Uri(url)));
                }
                httpResp.ResponseUri = httpRsp.RequestMessage.RequestUri.ToString();
                httpResp.Content     = httpRsp.Content.ReadAsStringAsync().Result;
                httpResp.StatusCode  = httpRsp.StatusCode;
            }
            return(httpResp);
        }