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; }