POST+JSON+XML

public static void getCityXml()
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string postdata = "requestMessage=%7B%22RCode%22%3A%22fengjing%40%23*%212014%22%2C

%22ClientType%22%3A0%2C%22Module%22%3A%22SmartTour%22%2C%22Method%22%3A%22GetRegionList%22%2C

%22Data%22%3A%7B%22RegionKey%22%3A%22%22%7D%7D";
            request = (HttpWebRequest)WebRequest.Create

(@"http://travel.fengjing.com/HolidaySvc.asmx/RequestServiceData");
            request.Method = "POST";
            request.Host = "travel.fengjing.com";
            request.KeepAlive = true;
            request.ContentLength = 202;
            request.ContentType = "application/x-www-form-urlencoded";

            byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
            request.ContentLength = postdatabytes.Length;
            Stream stream = request.GetRequestStream();
            stream.Write(postdatabytes, 0, postdatabytes.Length);
            stream.Close();

            response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string content = sr.ReadToEnd();
            sr.Close();
            request.Abort();
            response.Close();
            using (StreamWriter sw = new StreamWriter(@"D:\City.xml"))
            {
                sw.WriteLine(content);
            }
        }
        public static void getCityArray()
        {
            CityCount = 0;
            XmlDocument doc = new XmlDocument();
            doc.Load(@"D:\City.xml");
            XmlElement root = null;
            root = doc.DocumentElement;
            string str = "[" + root.InnerText + "]";//str为json字符串

            JArray jsonObj = JArray.Parse(str);
            JObject j = (JObject)jsonObj.First;
            j = (JObject)j["Data"];
            jsonObj = (JArray)j["Citys"];

            
            foreach (JObject jObject in jsonObj)
            {
                city[CityCount] = new City();
                city[CityCount].Initial = jObject["Initial"].ToString();
                city[CityCount].Code = jObject["Code"].ToString();
                city[CityCount].Parent = jObject["Parent"].ToString();
                city[CityCount].Name = jObject["Name"].ToString();
                city[CityCount].IsSmartTourCity = jObject["IsSmartTourCity"].ToString();
                CityCount++;
            }
            Console.WriteLine(CityCount);
            using (StreamWriter sw = new StreamWriter(@"C: \Users\Administrator\Desktop

\out.txt"))
            {
                for(int i = 0; i < CityCount; i++)
                {
                    sw.WriteLine(city[i].Initial+":"+ city[i].Code + ":" + city[i].Parent + ":" 

+ city[i].Name + ":" + city[i].IsSmartTourCity);
                }
            }

            /*
            using (StreamWriter sw = new StreamWriter(@"C: \Users\Administrator\Desktop

\out.txt"))
            {
                sw.WriteLine(str);
            }
            Console.WriteLine(j);
            */
        }

你可能感兴趣的:(C#获取网页)