Example #1
0
        public static async Task SendHttpRequestAsync(
            string url,
            Dictionary <string, string> param,
            Dictionary <string, string> files,
            List <Cookie> cookies,
            RequestMethodEnum method,
            int timeout_second,
            VoidFunc <HttpResponseMessage> handler)
        {
            var u = new Uri(url);

            using (var httpHandler = new HttpClientHandler()
            {
                UseCookies = false
            })
            {
                //创建cookie
                if (ValidateHelper.IsPlumpList(cookies))
                {
                    httpHandler.SetCookies(cookies, u);
                }
                using (var client = new HttpClient(httpHandler))
                {
                    client.DefaultRequestHeaders.Add("UserAgent",
                                                     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
                    //发送X_FORWARDED_FOR头(若是用取源IP的方式,可以用这个来造假IP,对日志的记录无效)
                    client.DefaultRequestHeaders.Add("X_FORWARDED_FOR", "101.0.0.11");
                    client.Timeout = TimeSpan.FromSeconds(timeout_second);

                    HttpResponseMessage response = null;
                    if (method == RequestMethodEnum.POST)
                    {
                        //使用MultipartFormDataContent请参考邮件里的记录
                        //拼url传参
                        //using (var urlContent = new FormUrlEncodedContent(param)) { }
                        //form提交
                        using (var formContent = new MultipartFormDataContent())
                        {
                            if (ValidateHelper.IsPlumpDict(param))
                            {
                                formContent.AddParam_(param);
                            }
                            if (ValidateHelper.IsPlumpDict(files))
                            {
                                foreach (var kv in files)
                                {
                                    formContent.AddFile_(kv.Key, kv.Value);
                                }
                            }
                            response = await client.PostAsync(u, formContent);
                        }
                    }
                    if (method == RequestMethodEnum.GET)
                    {
                        response = await client.GetAsync(u);
                    }
                    if (method == RequestMethodEnum.PUT)
                    {
                        throw new NotImplementedException();
                        //response = await client.PutAsync(u, null);
                    }
                    if (method == RequestMethodEnum.DELETE)
                    {
                        response = await client.DeleteAsync(u);
                    }

                    if (response == null)
                    {
                        throw new Exception("返回空");
                    }
                    using (response)
                    {
                        handler.Invoke(response);
                    }
                }
            }
        }