WebClient 请求返回的是Gbk编码解决方案

WebClient client = new WebClient();
                client.Headers.Clear();
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
                client.Encoding = Encoding.GetEncoding("gbk");
                string data = "&userCode = " + this.Property.userCode + " & userPwd = " + this.Property.userPwd + "&date=" + t.ToString("yyyy-MM-dd") +"&ifSearched="+this.Property.IfSearched;
                byte[] b = client.UploadData(this.Property.callUrl, "post", Encoding.GetEncoding("gbk").GetBytes(data));
                //var resultXml = this.Post(this.Property.callUrl,new {
                string resultXml= System.Text.Encoding.GetEncoding("gbk").GetString(b);
                this.LogInfo("上行返回:" + resultXml);
                resultXml = resultXml.CleanInvalidXmlChars();
                var doc = new System.Xml.XmlDocument();
                doc.LoadXml(resultXml);
                var headArray  = doc.SelectNodes("/response/head");
                string status = headArray[0].SelectSingleNode("code").InnerText;
                string body = doc.SelectNodes("/response/body")[0].InnerXml;
                if (status=="1"&&!string.IsNullOrWhiteSpace(body))
                {
                    var bodyArray  = doc.SelectNodes("/response/body/record");
                    foreach (System.Xml.XmlNode item in bodyArray)
                    {
                        string taskid= item.SelectSingleNode("deal_id").InnerText;
                        string mobile = item.SelectSingleNode("phone").InnerText;
                        string receivetime = item.SelectSingleNode("recv_date").InnerText;
                        string content = item.SelectSingleNode("content").InnerText;
                        this.LogInfo("获取的上行信息=" + "1:" + mobile + "\t2:" + taskid + "\t3:" + content + "\t4:" + receivetime  + "\r\n");
                        this.NotifyReceiveMo(mobile, content);
                    }
                }

posted on 2017-05-04 09:38  跨界农民工  阅读(400)  评论(0编辑  收藏  举报

导航