• Http Post


            public static async Task<string> PostFormUrlEncoded(string url, IEnumerable<KeyValuePair<string, string>> postData)
            {
                using (var httpClient = new HttpClient())
                {
                    using (var content = new FormUrlEncodedContent(postData))
                    {
                        content.Headers.Clear();
                        content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    
                        HttpResponseMessage response = await httpClient.PostAsync(url, content);
    
                        return await response.Content.ReadAsStringAsync();
                    }
                }
            }
    
            //public static string HttpPostForm(string url, IEnumerable<KeyValuePair<string, string>> pairs)
            //{
            //    var client = new RestClient(url);
            //    client.Timeout = 20 * 1000;
            //    var request = new RestRequest(Method.POST);
            //    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            //    foreach (var pair in pairs)
            //    {
            //        request.AddParameter(pair.Key, pair.Value);
            //    }
            //    IRestResponse response = client.Execute(request);
            //    return response.Content;
            //}
    
            public static string HttpPostFrom(string url, string data)
            {
                string htmlAll = "";
                try
                {
                    string SendMessageAddress = url;//请求链接
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SendMessageAddress);
                    request.Method = "POST";
                    request.AllowAutoRedirect = true;
                    request.Timeout = 20 * 1000;
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.Headers.Add("x-cherun-auth-key", "LarxMbndsxfGwoYAqsfJSPPU42l04cb3");
                    //string PostData = "a=1&b=2";//请求参数格式
                    string PostData = data;//请求参数
                    byte[] byteArray = Encoding.Default.GetBytes(PostData);
                    request.ContentLength = byteArray.Length;
                    using (Stream newStream = request.GetRequestStream())
                    {
                        newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                        newStream.Close();
                    }
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream rspStream = response.GetResponseStream();
                    using (StreamReader reader = new StreamReader(rspStream, Encoding.UTF8))
                    {
                        htmlAll = reader.ReadToEnd();
                        rspStream.Close();
                    }
                    response.Close();
                }
                catch (Exception ex)
                {
    
                    string s = ex.Message;
                }
                return htmlAll;
            }
  • 相关阅读:
    getRandomInt getRandomString
    git 换行符替换
    Versions maven plugin 修改版本
    spotless-maven-plugin java代码自动格式化mvn spotless:apply -fn
    eclipse.ini
    JVM架构和GC垃圾回收机制
    查看搜狗浏览器记住的密码
    TestGc finalize()
    Storm个人学习总结
    mongo嵌套查询
  • 原文地址:https://www.cnblogs.com/lopengye/p/15380149.html
Copyright © 2020-2023  润新知