Example #1
0
        public IActionResult backtoken()
        {
            var response = ResponseModelFactory.CreateResultInstance;
            var code     = "0";

            try
            {
                var    timeStamp   = GetTimeStamp();
                var    timepass    = timeStamp.ToString() + "123";
                string valPassword = GetMD5Hash(timepass);
                string serviceUrl  = "http://10.33.188.56:8090/addrMatch/auth/getToken?" + "user=abc&time=" + timeStamp + "&secret=" + valPassword;
                //response.SetData(serviceUrl);
                //return Ok(response);
                //创建Web访问对象
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
                //把用户传过来的数据转成“UTF-8”的字节流
                //byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);

                //myRequest.Method = "GET";
                //myRequest.ContentLength = buf.Length;
                myRequest.ContentType = "application/json";
                //myRequest.MaximumAutomaticRedirections = 1;
                //myRequest.AllowAutoRedirect = true;
                //发送请求
                //Stream stream = myRequest.GetRequestStream();
                //stream.Write(buf, 0, buf.Length);
                //stream.Close();

                //获取接口返回值
                //通过Web访问对象获取响应内容
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
                string returnJson = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
                code = myResponse.StatusCode.ToString();
                reader.Close();
                myResponse.Close();
                var    datatoken = JsonConvert.DeserializeObject <ViewModels.ToTkenInfo.Token>(returnJson);
                string token     = "";
                if (datatoken != null)
                {
                    token = datatoken.data.token;
                    using (_dbContext)
                    {
                        var sql = string.Format("TRUNCATE TABLE HomeAddress");
                        _dbContext.Database.ExecuteSqlRaw(sql);
                        var sno = Guid.NewGuid();
                        List <HaikanSmartTownCockpit.Api.Entities.HomeAddress> addresses = new List <HaikanSmartTownCockpit.Api.Entities.HomeAddress>();
                        string         infoUrl    = "http://10.33.188.56:8090/addrMatch/addrApi/searchAddr?token=" + token + "&addr=河桥&page=100&limit=9999&fuzzy=false&sno" + sno;
                        HttpWebRequest myRequest1 = (HttpWebRequest)WebRequest.Create(infoUrl);
                        myRequest1.ContentType = "application/json";
                        HttpWebResponse myResponse1 = (HttpWebResponse)myRequest1.GetResponse();
                        StreamReader    reader1     = new StreamReader(myResponse1.GetResponseStream(), Encoding.UTF8);
                        string          returnJson1 = reader1.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
                        code = myResponse1.StatusCode.ToString();
                        reader1.Close();
                        myResponse1.Close();
                        var data1 = JsonConvert.DeserializeObject <ViewModels.AddresInfo.AddressInfo>(returnJson1);
                        //response.SetData(data1);
                        //return Ok(response);
                        for (int j = 0; j < data1.data.addrList.Count; j++)
                        {
                            //var entity = new HaikanSmartTownCockpit.Api.Entities.HomeAddress();
                            HaikanSmartTownCockpit.Api.Entities.HomeAddress entity = new HaikanSmartTownCockpit.Api.Entities.HomeAddress();
                            entity.Address     = data1.data.addrList[j].addr;
                            entity.Addresscode = data1.data.addrList[j].code;
                            entity.Town        = data1.data.addrList[j].town;
                            entity.Ccmmunity   = data1.data.addrList[j].community;
                            entity.Squad       = data1.data.addrList[j].squad;
                            entity.Village     = data1.data.addrList[j].village;
                            entity.Szone       = data1.data.addrList[j].szone;
                            entity.Street      = data1.data.addrList[j].street;
                            entity.Door        = data1.data.addrList[j].door;
                            entity.Resregion   = data1.data.addrList[j].resregion;
                            entity.Building    = data1.data.addrList[j].building;
                            entity.BuildingNum = data1.data.addrList[j].building_num;
                            entity.Unit        = data1.data.addrList[j].unit;
                            entity.Floor       = data1.data.addrList[j].floor;
                            //entity.Room = data1.data.addrList[j].room;
                            //entity.RoomFloor = data1.data.addrList[j].room_floor;
                            entity.HomeAddressUuid = Guid.NewGuid();
                            entity.IsDeleted       = 0;
                            addresses.Add(entity);
                        }
                        _dbContext.AddRange(addresses);
                        _dbContext.SaveChanges();
                    }
                }
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.SetFailed("获取失败");
                response.SetData(ex.Message);
                return(Ok(response));
            }
        }