当前位置: 首页>>代码示例>>C#>>正文


C# FormUrlEncodedContent.LoadIntoBufferAsync方法代码示例

本文整理汇总了C#中System.Net.Http.FormUrlEncodedContent.LoadIntoBufferAsync方法的典型用法代码示例。如果您正苦于以下问题:C# FormUrlEncodedContent.LoadIntoBufferAsync方法的具体用法?C# FormUrlEncodedContent.LoadIntoBufferAsync怎么用?C# FormUrlEncodedContent.LoadIntoBufferAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.FormUrlEncodedContent的用法示例。


在下文中一共展示了FormUrlEncodedContent.LoadIntoBufferAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SendBaidu

        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;
         
        }
开发者ID:antaintan,项目名称:BaiduPushSDK,代码行数:48,代码来源:Baidu_Helper.cs

示例2: SendBaidu

        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;

        }
开发者ID:wawa0210,项目名称:BaiduPush28,代码行数:65,代码来源:Baidu_Helper.cs


注:本文中的System.Net.Http.FormUrlEncodedContent.LoadIntoBufferAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。