{"id":"239","uid":"1","name":"api_csharp","title":"C#\u6e90\u7801\uff08API\uff09","category_id":"282","description":"","root":"0","pid":"0","model_id":"2","type":"2","position":"0","link_id":"0","cover_id":"0","display":"1","deadline":"0","attach":"0","view":"0","comment":"0","extend":"0","level":"0","create_time":"1473062460","update_time":"1473062557","status":"1","parse":"0","content":"
     private void button1_Click(object sender, EventArgs e)\r\n {\r\n     string server_addr =\"http:\/\/192.168.1.99\";\t \t\t\/\/\u670d\u52a1\u5668\u5730\u5740\r\n            if (server_addr==\"\")\r\n            {\r\n                MessageBox.Show(\"\u670d\u52a1\u5668\u5730\u5740\u4e0d\u80fd\u4e3a\u7a7a \");\r\n                return;\r\n            }\r\n            string api_key = \"2f5er1f5fghnj324kkmbvkasmdkfffdd\";\t\/\/API_key\r\n            string api_name = \"\/Api\/Book\/info\";          \t\t\/\/\u83b7\u53d6\u56fe\u4e66\u8d44\u6599\本\u636e\r\n\r\n            Dictionary<string, string> get_param_arr = new Dictionary<string, string>();\r\n            get_param_arr.Add(\"barcode\", \"000000026\");\t\t\t\/\/\u4f20\u9012\u6761\u7801\r\n\r\n            string api_sign = this.genApiSign(api_key, get_param_arr);\t\/\/\u751f\u6210MD5\u7b7e\u540d\r\n            get_param_arr.Add(\"signstr\", api_sign);\r\n\r\n            string httpParam = \"\";\r\n            foreach (var item in get_param_arr)\r\n            {\r\n                string tmp_str = item.Key + \"=\" + item.Value;\r\n                if (httpParam.Length > 0)\r\n                {\r\n                    httpParam += \"&\";\r\n                }\r\n                httpParam += tmp_str;\r\n            }\r\n\r\n            string ret_str = this.HttpGet(server_addr + api_name, httpParam); \r\n            textBox2.Text = ret_str;\r\n\r\n            \/\/JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aDictionary<string, object>\r\n            Dictionary<string, object> ret_list = this.parseJsonStr(ret_str);\r\n\r\n\r\n }\r\n\r\n \/*\u751f\u6210api\u8bf7\u6c42\u5b57\u7b26\u4e32\r\n * @param string appkey \u6388\u6743key\r\n * @param Dictionary param_list \u53c2\本\本\u7ec4\r\n * @return string \u8bf7\u6c42\u5b57\u7b26\u4e32\r\n *\/\r\n public string genApiSign(string appkey, Dictionary param_list)\r\n {\r\n string ret_str = \"\";\r\n string param_str = \"\";\r\n foreach (var item in param_list)\r\n {\r\n string tmp_str=item.Key + \"=\" + item.Value;\r\n if (param_str.Length>0)\r\n {\r\n param_str += \"&\";\r\n }\r\n param_str += tmp_str;\r\n }\r\n param_str += appkey; \r\n\r\n byte[] result = Encoding.Default.GetBytes(param_str); \/\/tbPass\u4e3a\u8f93\u5165\u5bc6\u7801\体育\u6587\u672c\u6846\r\n MD5 md5 = new MD5CryptoServiceProvider();\r\n byte[] output = md5.ComputeHash(result);\r\n ret_str = BitConverter.ToString(output).Replace(\"-\", \"\");\r\n ret_str = ret_str.ToLower();\r\n return ret_str;\r\n }\r\n\r\n public string HttpGet(string Url, string postDataStr)\r\n {\r\n HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == \"\" ? \"\" : \"?\") + postDataStr);\r\n request.Method = \"GET\";\r\n request.ContentType = \"text\/html;charset=UTF-8\";\r\n\r\n HttpWebResponse response = (HttpWebResponse)request.GetResponse();\r\n Stream myResponseStream = response.GetResponseStream();\r\n StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(\"utf-8\"));\r\n string retString = myStreamReader.ReadToEnd();\r\n myStreamReader.Close();\r\n myResponseStream.Close();\r\n\r\n return retString;\r\n }\r\n\r\n\r\n\r\n public Dictionary parseJsonStr(string jsonString)\r\n {\r\n \/\/\u5b9e\u4f8b\u5316JavaScriptSerializer\u7c7b\体育\u65b0\u5b9e\u4f8b\r\n\r\n JavaScriptSerializer jss = new JavaScriptSerializer();\r\n\r\n try\r\n {\r\n\r\n \/\/\u5c06\u6307\u5b9a\体育 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Dictionary \u7c7b\u578b\体育\u5bf9\u8c61\r\n\r\n return jss.Deserialize>(jsonString);\r\n\r\n }\r\n\r\n catch (Exception ex)\r\n {\r\n\r\n throw new Exception(ex.Message);\r\n\r\n }\r\n \r\n }<\/dictionary<\/string,><\/string,><\/string,><\/string,><\/string,><\/string,><\/string,><\/string,><\/pre>","template":"","bookmark":"0"}