Example #1
0
        public static async Task <string> SendBaidu(string httpMethod, string url, string secretKey, Baidu_Mod mod)
        {
            //获取签名
            string strSign = CreateSign(httpMethod, url, secretKey, mod);

            string responseContent = "";                //返回字符串

            //使用本机代理fiddler进行测试
            //var handler = new HttpClientHandler()
            //{
            //    Proxy = new WebProxy("http://127.0.0.1:8888", false, new string[] { }),
            //    UseProxy = true
            //};

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(url);                                                                                  //设置WebAPI的地址+类+方法
                client.DefaultRequestHeaders.Accept.Clear();                                                                        //清空缓存
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")
                {
                    CharSet = "utf-8"
                });                                                                                                                 //返回值为json和utf-8编码

                //响应消息
                HttpResponseMessage response = null;

                //判断使用不同的HttpMethod,生成不同的参数,获取相关返回数据
                if (HttpMethod.Get.Method.ToLower().Equals(httpMethod.ToLower()))                                   //get方法(暂不提供)
                {
                }
                else if (HttpMethod.Post.Method.ToLower().Equals(httpMethod.ToLower()))                             //post方法
                {
                    //生成post的参数,并且设置charset为utf-8
                    Dictionary <string, string> requestDictionary = GetParamerCollection(mod, strSign);
                    var contents = new FormUrlEncodedContent(requestDictionary);
                    contents.Headers.ContentType.CharSet = "utf-8";
                    await contents.LoadIntoBufferAsync();

                    response = await client.PostAsync(url, contents);
                }

                //返回值
                responseContent = await response.Content.ReadAsStringAsync();
            }// http完结

            return(responseContent);
        }
Example #2
0
        public static async Task <string> SendBaidu(string httpMethod, string url, string secretKey, Baidu_Mod mod)
        {
            //获取签名
            string strSign = CreateSign(httpMethod, url, secretKey, mod);

            string responseContent = "";                //返回字符串

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(url);                                                                                  //设置WebAPI的地址+类+方法
                client.DefaultRequestHeaders.Accept.Clear();                                                                        //清空缓存
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")
                {
                    CharSet = "utf-8"
                });                                                                                                                      //返回值为json和utf-8编码

                //响应消息
                HttpResponseMessage response = null;

                //判断使用不同的HttpMethod,生成不同的参数,获取相关返回数据
                if (HttpMethod.Get.Method.ToLower().Equals(httpMethod.ToLower())) //get方法(暂不提供)
                {
                }
                else if (HttpMethod.Post.Method.ToLower().Equals(httpMethod.ToLower()))                             //post方法
                {
                    //生成post的参数,并且设置charset为utf-8
                    Dictionary <string, string> requestDictionary = GetParamerCollection(mod, strSign);
                    var contents = new FormUrlEncodedContent(requestDictionary);
                    contents.Headers.ContentType.CharSet = "utf-8";
                    await contents.LoadIntoBufferAsync();

                    try
                    {
                        response = await client.PostAsync(url, contents);

                        //返回值
                        responseContent = await response.Content.ReadAsStringAsync();
                    }
                    catch (WebException ex)
                    {
                        Stream stream = ex.Response.GetResponseStream();
                        string m      = ex.Response.Headers.ToString();
                        byte[] buf    = new byte[256];
                        stream.Read(buf, 0, 256);
                        stream.Close();
                        int count = 0;
                        foreach (var b in buf)
                        {
                            if (b > 0)
                            {
                                count++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        responseContent = "Post:" + url + ex.Message + "\r\n\r\n" + Encoding.UTF8.GetString(buf, 0, count);
                    }
                }
            }

            return(responseContent);
        }