using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Security.Cryptography; namespace ConsoleApp { class Program { static void Main(string[] args) { Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("access_token", "e.g accesstoken"); parameters.Add("method", "xiaonei.blog.addBlog"); parameters.Add("format", "json"); parameters.Add("title", "abcdadf"); parameters.Add("content", "contentsdfsd"); parameters.Add("v", "1.0"); string sig = GenerateSignature(parameters); parameters.Add("sig", sig); string post_string = GeneratePostParameter(parameters); Console.WriteLine("POST参数值:\n" + post_string + "\n"); post_string = HttpWorker.HttpPost("http://api.renren.com/restserver.do", post_string); Console.WriteLine("POST返回结果:\n" + post_string); Console.ReadLine(); } static string GenerateSignature(Dictionary<string, string> parameters) { IDictionary<string, string> sorted_paramters = new SortedDictionary<string, string>(parameters); IEnumerator<KeyValuePair<string, string>> dem = sorted_paramters.GetEnumerator(); StringBuilder sbQuery = new StringBuilder(); while (dem.MoveNext()) { string name = dem.Current.Key; string value = dem.Current.Value; if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value)) { sbQuery.Append(name); sbQuery.Append("="); sbQuery.Append(value); } } sbQuery.Append("your secret key"); Console.WriteLine("拼接参数值:\n" + sbQuery.ToString() + "\n"); return HashConfigString(sbQuery.ToString()); } static string GeneratePostParameter(Dictionary<string ,string> parameters) { bool hasParameter = false; StringBuilder sbPostString = new StringBuilder(); IDictionary<string, string> sorted_paramters = new SortedDictionary<string, string>(parameters); IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator(); while (dem.MoveNext()) { string name = dem.Current.Key; string value = dem.Current.Value; if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value)) { if (hasParameter) { sbPostString.Append("&"); } sbPostString.Append(name); sbPostString.Append("="); sbPostString.Append(value); hasParameter = true; } } return sbPostString.ToString(); } static string HashConfigString(string plain_text) { MD5 md5Hash = MD5.Create(); byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(plain_text)); StringBuilder sbContent = new StringBuilder(); foreach (byte element in data) { sbContent.Append(element.ToString("x2")); } Console.WriteLine("sig:\n" + sbContent.ToString() + "\n"); return sbContent.ToString(); } } public class HttpWorker { public static string HttpGet(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()); string http_result = reader.ReadToEnd(); if (reader != null) reader.Close(); return http_result; } public static string HttpPost(string url, string data) { //System.Net.ServicePointManager.Expect100Continue = false; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.KeepAlive = false; // request.ServicePoint.Expect100Continue = false; request.ContentType = "application/x-www-form-urlencoded;"; byte[] post = Encoding.UTF8.GetBytes(data); using (Stream stream = request.GetRequestStream()) { stream.Write(post, 0, post.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string http_result = string.Empty; StreamReader reader = new StreamReader(response.GetResponseStream()); http_result = reader.ReadToEnd(); if (reader != null) reader.Close(); if (response != null) response.Close(); return http_result; } } } }