微信公众平台消息接口开发 快递查询
string str = ""; WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。 Byte[] Comp = MyWebClient.DownloadData(Url1); //获取快递公司数据 var CompHtml = Encoding.UTF8.GetString(Comp); //如果获取网站页面采用的是UTF-8,则使用这句 try { JavaScriptSerializer Compjs = new JavaScriptSerializer(); var compObj = Compjs.DeserializeObject(CompHtml); Dictionary<string, object> Compdic = ((Object[])compObj).ToArray()[0] as Dictionary<string, object>; string comCode = Compdic["comCode"].ToString();//查询的公司 申通、圆通等 Byte[] pageData = MyWebClient.DownloadData(Url2); //从指定网站下载数据 var pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句 JavaScriptSerializer js = new JavaScriptSerializer(); var obj = js.DeserializeObject(pageHtml); Dictionary<string, object> dic = obj as Dictionary<string, object>; var message = dic["message"].ToString(); if (message == "ok") { for (int i = ((object[])dic.Values.ToArray()[7]).Length - 1; i >= 0; i--) { Dictionary<string, object> temqp = ((object[])dic.Values.ToArray()[7]).ToArray()[i] as Dictionary<string, object>; str += temqp["ftime"] + "\n"; str += temqp["context"] + "\n"; } } else { str = "[流泪] Sorry,你要查询的快递暂时没有找到,请稍后再试~~"; } } catch (Exception) { str = "[流泪] Sorry,你的快递单号输入有误。亲,仔细核对下单号哟~"; } return str;