• WebResponse 取出全国省市区的邮编


    WebResponse用法(根据省市区地址查询其邮编):

    class Program
        {
            static string url { get; set; }
            static void Main(string[] args)
            {
                StringBuilder sb = new StringBuilder();
                try
                {
                    SqlConnection conn = new SqlConnection("自己的数据库连接语句");
                    SqlDataAdapter da = new SqlDataAdapter("查询语句", conn);
                    DataSet ds = new DataSet();
                    Random rd = new Random();
                    da.Fill(ds);
                    url = "http://opendata.baidu.com/post/s?wd={0}&rn=20";//请求地址
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        var row = ds.Tables[0].Rows[i];
                        string keyworder = row["ProvinceName"] + " " + row["CityName"] + " " + row["DistrictName"];
                        sb.Append(GetZipCodeSql(keyworder, row["SysNo"].ToString()));
    
                        Thread.Sleep(rd.Next(0, 1000));//延时
                        if (i != 0 && i % 100 == 0)
                        {
                            WriteTxt(sb.ToString());
    
                            sb.Clear();
                        }
                        else if (i == ds.Tables[0].Rows.Count - 1)
                        {
                            WriteTxt(sb.ToString());
    
                            sb.Clear();
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                }
            }
            private static void WriteTxt(string text)
            {
                StreamWriter sw = File.AppendText("D:\3.txt");//保存输出的位置
                sw.Write(text);
                sw.Close();
            }
        //得到邮编
    private static string GetZipCodeSql(string keyworder, string sysNo) { StringBuilder sb = new StringBuilder(); try { HttpWebRequest httpweb = (HttpWebRequest)WebRequest.Create( new Uri(string.Format(url, HttpUtility.UrlEncode(keyworder, System.Text.Encoding.GetEncoding("GBK")))) ); WebResponse respon = httpweb.GetResponse(); StreamReader reader = new StreamReader(respon.GetResponseStream(), Encoding.GetEncoding("GBK")); string html = reader.ReadToEnd(); int statrtIndex = 0; int end = 0; string code = ""; statrtIndex = html.IndexOf("<ul><!-- baidu-tc begin -->"); if (statrtIndex > 0) { end = html.IndexOf("</a>", statrtIndex); statrtIndex = html.LastIndexOf("</em>", end); code = Regex.Replace(html.Substring(end - 22, 22), @"[^0-9]", ""); //code = html.Substring(end - 6, 6); sb.AppendFormat("UPDATE area SET ZipCode ={0} WHERE SysNo={1}; ", code, sysNo); } respon.Close(); } catch (Exception) { url = "http://opendata.baidu.com/post/s?wd={0}&p=mini&rn=20"; GetZipCodeSql(keyworder, sysNo); } return sb.ToString(); } }

     附:全国省市区.zip

  • 相关阅读:
    Bootstrap 学习笔记 之网格系统
    自定义属性建立匹配、对应关系——JS学习笔记2015-5-27(第40天)
    CSS那些事儿——关于如何提高编写代码的能力
    自定义属性,匹配数组内容——JS学习笔记2015-5-26(第39天)
    css学习之幻灯片(轮播图)原理
    自定义属性,第一节——JS学习笔记2015-5-25(第38天)
    字体不同,造成的显示异常
    浮动相关理解,以及清除浮动的方法总结
    border实现三角箭头
    go mode
  • 原文地址:https://www.cnblogs.com/LiuLiangXuan/p/4623761.html
Copyright © 2020-2023  润新知