使用WebRequest 检测 手机号归属地。 C#通用 使用json 和可设定超时的WebClient

首先建立jsonObject,当然你也可以使用xml解析,目前介绍一下我使用的方法。

[csharp] view plaincopyprint?
 
  1. /********************************************************** 
  2.  * 说明:Json通用转换类 
  3.  *********************************************************/  
  4.   
  5. using System;  
  6. using System.Collections.Generic;  
  7. using System.Text;  
  8.   
  9. namespace Xfrog.Net  
  10. {  
  11.   
  12.     /// <summary>  
  13.     /// 用于构建属性值的回调  
  14.     /// </summary>  
  15.     /// <param name="Property"></param>  
  16.     public delegate void SetProperties(JsonObject Property);  
  17.   
  18.     /// <summary>  
  19.     /// JsonObject属性值类型  
  20.     /// </summary>  
  21.     public enum JsonPropertyType  
  22.     {  
  23.         String,  
  24.         Object,  
  25.         Array,  
  26.         Number,  
  27.         Bool,  
  28.         Null  
  29.     }  
  30.   
  31.     /// <summary>  
  32.     /// JSON通用对象  
  33.     /// </summary>  
  34.     public class JsonObject  
  35.     {  
  36.         private Dictionary<String, JsonProperty> _property;  
  37.   
  38.         public JsonObject()  
  39.         {  
  40.             this._property = null;  
  41.         }  
  42.   
  43.         public JsonObject(String jsonString)  
  44.         {  
  45.             this.Parse(ref jsonString);  
  46.         }  
  47.   
  48.         public JsonObject(SetProperties callback)  
  49.         {  
  50.             if (callback != null)  
  51.             {  
  52.                 callback(this);  
  53.             }  
  54.         }  
  55.   
  56.         /// <summary>  
  57.         /// Json字符串解析  
  58.         /// </summary>  
  59.         /// <param name="jsonString"></param>  
  60.         private void Parse(ref String jsonString)  
  61.         {  
  62.             int len = jsonString.Length;  
  63.             if (String.IsNullOrEmpty(jsonString) || jsonString.Substring(0, 1) != "{" || jsonString.Substring(jsonString.Length - 1, 1) != "}")  
  64.             {  
  65.                 throw new ArgumentException("传入文本不符合Json格式!" + jsonString);  
  66.             }  
  67.             Stack<Char> stack = new Stack<char>();  
  68.             Stack<Char> stackType = new Stack<char>();  
  69.             StringBuilder sb = new StringBuilder();  
  70.             Char cur;  
  71.             bool convert = false;  
  72.             bool isValue = false;  
  73.             JsonProperty last = null;  
  74.             for (int i = 1; i <= len - 2; i++)  
  75.             {  
  76.                 cur = jsonString[i];  
  77.                 if (cur == '}')  
  78.                 {  
  79.                     ;  
  80.                 }  
  81.                 if (cur == ' ' && stack.Count == 0)  
  82.                 {  
  83.                     ;  
  84.                 }  
  85.                 else if ((cur == '\'' || cur == '\"') && !convert && stack.Count == 0 && !isValue)  
  86.                 {  
  87.                     sb.Length = 0;  
  88.                     stack.Push(cur);  
  89.                 }  
  90.                 else if ((cur == '\'' || cur == '\"') && !convert && stack.Count > 0 && stack.Peek() == cur && !isValue)  
  91.                 {  
  92.                     stack.Pop();  
  93.                 }  
  94.                 else if ((cur == '[' || cur == '{') && stack.Count == 0)  
  95.                 {  
  96.                     stackType.Push(cur == '[' ? ']' : '}');  
  97.                     sb.Append(cur);  
  98.                 }  
  99.                 else if ((cur == ']' || cur == '}') && stack.Count == 0 && stackType.Peek() == cur)  
  100.                 {  
  101.                     stackType.Pop();  
  102.                     sb.Append(cur);  
  103.                 }  
  104.                 else if (cur == ':' && stack.Count == 0 && stackType.Count == 0 && !isValue)  
  105.                 {  
  106.                     last = new JsonProperty();  
  107.                     this[sb.ToString()] = last;  
  108.                     isValue = true;  
  109.                     sb.Length = 0;  
  110.                 }  
  111.                 else if (cur == ',' && stack.Count == 0 && stackType.Count == 0)  
  112.                 {  
  113.                     if (last != null)  
  114.                     {  
  115.   
  116.                         String temp = sb.ToString();  
  117.                         last.Parse(ref temp);  
  118.                     }  
  119.                     isValue = false;  
  120.                     sb.Length = 0;  
  121.                 }  
  122.                 else  
  123.                 {  
  124.                     sb.Append(cur);  
  125.                 }  
  126.             }  
  127.             if (sb.Length > 0 && last != null && last.Type == JsonPropertyType.Null)  
  128.             {  
  129.                 String temp = sb.ToString();  
  130.                 last.Parse(ref temp);  
  131.             }  
  132.         }  
  133.   
  134.         /// <summary>  
  135.         /// 获取属性  
  136.         /// </summary>  
  137.         /// <param name="PropertyName"></param>  
  138.         /// <returns></returns>  
  139.         public JsonProperty this[String PropertyName]  
  140.         {  
  141.             get  
  142.             {  
  143.                 JsonProperty result = null;  
  144.                 if (this._property != null && this._property.ContainsKey(PropertyName))  
  145.                 {  
  146.                     result = this._property[PropertyName];  
  147.                 }  
  148.                 return result;  
  149.             }  
  150.             set  
  151.             {  
  152.                 if (this._property == null)  
  153.                 {  
  154.                     this._property = new Dictionary<string, JsonProperty>(StringComparer.OrdinalIgnoreCase);  
  155.                 }  
  156.                 if (this._property.ContainsKey(PropertyName))  
  157.                 {  
  158.                     this._property[PropertyName] = value;  
  159.                 }  
  160.                 else  
  161.                 {  
  162.                     this._property.Add(PropertyName, value);  
  163.                 }  
  164.             }  
  165.         }  
  166.   
  167.         /// <summary>  
  168.         /// 通过此泛型函数可直接获取指定类型属性的值  
  169.         /// </summary>  
  170.         /// <typeparam name="T"></typeparam>  
  171.         /// <param name="PropertyName"></param>  
  172.         /// <returns></returns>  
  173.         public virtual T Properties<T>(String PropertyName) where T : class  
  174.         {  
  175.             JsonProperty p = this[PropertyName];  
  176.             if (p != null)  
  177.             {  
  178.                 return p.GetValue<T>();  
  179.             }  
  180.             return default(T);  
  181.         }  
  182.   
  183.         /// <summary>  
  184.         /// 获取属性名称列表  
  185.         /// </summary>  
  186.         /// <returns></returns>  
  187.         public String[] GetPropertyNames()  
  188.         {  
  189.             if (this._property == null)  
  190.                 return null;  
  191.             String[] keys = null;  
  192.             if (this._property.Count > 0)  
  193.             {  
  194.                 keys = new String[this._property.Count];  
  195.                 this._property.Keys.CopyTo(keys, 0);  
  196.             }  
  197.             return keys;  
  198.         }  
  199.   
  200.         /// <summary>  
  201.         /// 移除一个属性  
  202.         /// </summary>  
  203.         /// <param name="PropertyName"></param>  
  204.         /// <returns></returns>  
  205.         public JsonProperty RemoveProperty(String PropertyName)  
  206.         {  
  207.             if (this._property != null && this._property.ContainsKey(PropertyName))  
  208.             {  
  209.                 JsonProperty p = this._property[PropertyName];  
  210.                 this._property.Remove(PropertyName);  
  211.                 return p;  
  212.             }  
  213.             return null;  
  214.         }  
  215.   
  216.         /// <summary>  
  217.         /// 是否为空对象  
  218.         /// </summary>  
  219.         /// <returns></returns>  
  220.         public bool IsNull()  
  221.         {  
  222.             return this._property == null;  
  223.         }  
  224.   
  225.         public override string ToString()  
  226.         {  
  227.             return this.ToString("");  
  228.         }  
  229.   
  230.         /// <summary>  
  231.         /// ToString...  
  232.         /// </summary>  
  233.         /// <param name="format">格式化字符串</param>  
  234.         /// <returns></returns>  
  235.         public virtual string ToString(String format)  
  236.         {  
  237.             if (this.IsNull())  
  238.             {  
  239.                 return "{}";  
  240.             }  
  241.             else  
  242.             {  
  243.                 StringBuilder sb = new StringBuilder();  
  244.                 foreach (String key in this._property.Keys)  
  245.                 {  
  246.                     sb.Append(",");  
  247.                     sb.Append(key).Append(": ");  
  248.                     sb.Append(this._property[key].ToString(format));  
  249.   
  250.                 }  
  251.                 if (this._property.Count > 0)  
  252.                 {  
  253.                     sb.Remove(0, 1);  
  254.                 }  
  255.                 sb.Insert(0, "{");  
  256.                 sb.Append("}");  
  257.                 return sb.ToString();  
  258.             }  
  259.         }  
  260.     }  
  261.   
  262.     /// <summary>  
  263.     /// JSON对象属性  
  264.     /// </summary>  
  265.     public class JsonProperty  
  266.     {  
  267.         private JsonPropertyType _type;  
  268.         private String _value;  
  269.         private JsonObject _object;  
  270.         private List<JsonProperty> _list;  
  271.         private bool _bool;  
  272.         private double _number;  
  273.   
  274.         public JsonProperty()  
  275.         {  
  276.             this._type = JsonPropertyType.Null;  
  277.             this._value = null;  
  278.             this._object = null;  
  279.             this._list = null;  
  280.         }  
  281.   
  282.         public JsonProperty(Object value)  
  283.         {  
  284.             this.SetValue(value);  
  285.         }  
  286.   
  287.   
  288.         public JsonProperty(String jsonString)  
  289.         {  
  290.             this.Parse(ref jsonString);  
  291.         }  
  292.   
  293.   
  294.         /// <summary>  
  295.         /// Json字符串解析  
  296.         /// </summary>  
  297.         /// <param name="jsonString"></param>  
  298.         public void Parse(ref String jsonString)  
  299.         {  
  300.             if (String.IsNullOrEmpty(jsonString))  
  301.             {  
  302.                 this.SetValue(null);  
  303.             }  
  304.             else  
  305.             {  
  306.                 string first = jsonString.Substring(0, 1);  
  307.                 string last = jsonString.Substring(jsonString.Length - 1, 1);  
  308.                 if (first == "[" && last == "]")  
  309.                 {  
  310.                     this.SetValue(this.ParseArray(ref jsonString));  
  311.                 }  
  312.                 else if (first == "{" && last == "}")  
  313.                 {  
  314.                     this.SetValue(this.ParseObject(ref jsonString));  
  315.                 }  
  316.                 else if ((first == "'" || first == "\"") && first == last)  
  317.                 {  
  318.                     this.SetValue(this.ParseString(ref jsonString));  
  319.                 }  
  320.                 else if (jsonString == "true" || jsonString == "false")  
  321.                 {  
  322.                     this.SetValue(jsonString == "true" ? true : false);  
  323.                 }  
  324.                 else if (jsonString == "null")  
  325.                 {  
  326.                     this.SetValue(null);  
  327.                 }  
  328.                 else  
  329.                 {  
  330.                     double d = 0;  
  331.                     if (double.TryParse(jsonString, out d))  
  332.                     {  
  333.                         this.SetValue(d);  
  334.                     }  
  335.                     else  
  336.                     {  
  337.                         this.SetValue(jsonString);  
  338.                     }  
  339.                 }  
  340.             }  
  341.         }  
  342.   
  343.         /// <summary>  
  344.         /// Json Array解析  
  345.         /// </summary>  
  346.         /// <param name="jsonString"></param>  
  347.         /// <returns></returns>  
  348.         private List<JsonProperty> ParseArray(ref String jsonString)  
  349.         {  
  350.             List<JsonProperty> list = new List<JsonProperty>();  
  351.             int len = jsonString.Length;  
  352.             StringBuilder sb = new StringBuilder();  
  353.             Stack<Char> stack = new Stack<char>();  
  354.             Stack<Char> stackType = new Stack<Char>();  
  355.             bool conver = false;  
  356.             Char cur;  
  357.             for (int i = 1; i <= len - 2; i++)  
  358.             {  
  359.                 cur = jsonString[i];  
  360.                 if (Char.IsWhiteSpace(cur) && stack.Count == 0)  
  361.                 {  
  362.                     ;  
  363.                 }  
  364.                 else if ((cur == '\'' && stack.Count == 0 && !conver && stackType.Count == 0) || (cur == '\"' && stack.Count == 0 && !conver && stackType.Count == 0))  
  365.                 {  
  366.                     sb.Length = 0;  
  367.                     sb.Append(cur);  
  368.                     stack.Push(cur);  
  369.                 }  
  370.                 else if (cur == '\\' && stack.Count > 0 && !conver)  
  371.                 {  
  372.                     sb.Append(cur);  
  373.                     conver = true;  
  374.                 }  
  375.                 else if (conver == true)  
  376.                 {  
  377.                     conver = false;  
  378.   
  379.                     if (cur == 'u')  
  380.                     {  
  381.                         sb.Append(new char[] { cur, jsonString[i + 1], jsonString[i + 2], jsonString[i + 3] });  
  382.                         i += 4;  
  383.                     }  
  384.                     else  
  385.                     {  
  386.                         sb.Append(cur);  
  387.                     }  
  388.                 }  
  389.                 else if ((cur == '\'' || cur == '\"') && !conver && stack.Count > 0 && stack.Peek() == cur && stackType.Count == 0)  
  390.                 {  
  391.                     sb.Append(cur);  
  392.                     list.Add(new JsonProperty(sb.ToString()));  
  393.                     stack.Pop();  
  394.                 }  
  395.                 else if ((cur == '[' || cur == '{') && stack.Count == 0)  
  396.                 {  
  397.                     if (stackType.Count == 0)  
  398.                     {  
  399.                         sb.Length = 0;  
  400.                     }  
  401.                     sb.Append(cur);  
  402.                     stackType.Push((cur == '[' ? ']' : '}'));  
  403.                 }  
  404.                 else if ((cur == ']' || cur == '}') && stack.Count == 0 && stackType.Count > 0 && stackType.Peek() == cur)  
  405.                 {  
  406.                     sb.Append(cur);  
  407.                     stackType.Pop();  
  408.                     if (stackType.Count == 0)  
  409.                     {  
  410.                         list.Add(new JsonProperty(sb.ToString()));  
  411.                         sb.Length = 0;  
  412.                     }  
  413.   
  414.                 }  
  415.                 else if (cur == ',' && stack.Count == 0 && stackType.Count == 0)  
  416.                 {  
  417.                     if (sb.Length > 0)  
  418.                     {  
  419.                         list.Add(new JsonProperty(sb.ToString()));  
  420.                         sb.Length = 0;  
  421.                     }  
  422.                 }  
  423.                 else  
  424.                 {  
  425.                     sb.Append(cur);  
  426.                 }  
  427.             }  
  428.             if (stack.Count > 0 || stackType.Count > 0)  
  429.             {  
  430.                 list.Clear();  
  431.                 throw new ArgumentException("无法解析Json Array对象!");  
  432.             }  
  433.             else if (sb.Length > 0)  
  434.             {  
  435.                 list.Add(new JsonProperty(sb.ToString()));  
  436.             }  
  437.             return list;  
  438.         }  
  439.   
  440.   
  441.         /// <summary>  
  442.         /// Json String解析  
  443.         /// </summary>  
  444.         /// <param name="jsonString"></param>  
  445.         /// <returns></returns>  
  446.         private String ParseString(ref String jsonString)  
  447.         {  
  448.             int len = jsonString.Length;  
  449.             StringBuilder sb = new StringBuilder();  
  450.             bool conver = false;  
  451.             Char cur;  
  452.             for (int i = 1; i <= len - 2; i++)  
  453.             {  
  454.                 cur = jsonString[i];  
  455.                 if (cur == '\\' && !conver)  
  456.                 {  
  457.                     conver = true;  
  458.                 }  
  459.                 else if (conver == true)  
  460.                 {  
  461.                     conver = false;  
  462.                     if (cur == '\\' || cur == '\"' || cur == '\'' || cur == '/')  
  463.                     {  
  464.                         sb.Append(cur);  
  465.                     }  
  466.                     else  
  467.                     {  
  468.                         if (cur == 'u')  
  469.                         {  
  470.                             String temp = new String(new char[] { cur, jsonString[i + 1], jsonString[i + 2], jsonString[i + 3] });  
  471.                             sb.Append((char)Convert.ToInt32(temp, 16));  
  472.                             i += 4;  
  473.                         }  
  474.                         else  
  475.                         {  
  476.                             switch (cur)  
  477.                             {  
  478.                                 case 'b':  
  479.                                     sb.Append('\b');  
  480.                                     break;  
  481.                                 case 'f':  
  482.                                     sb.Append('\f');  
  483.                                     break;  
  484.                                 case 'n':  
  485.                                     sb.Append('\n');  
  486.                                     break;  
  487.                                 case 'r':  
  488.                                     sb.Append('\r');  
  489.                                     break;  
  490.                                 case 't':  
  491.                                     sb.Append('\t');  
  492.                                     break;  
  493.                             }  
  494.                         }  
  495.                     }  
  496.                 }  
  497.                 else  
  498.                 {  
  499.                     sb.Append(cur);  
  500.                 }  
  501.             }  
  502.             return sb.ToString();  
  503.         }  
  504.   
  505.         /// <summary>  
  506.         /// Json Object解析  
  507.         /// </summary>  
  508.         /// <param name="jsonString"></param>  
  509.         /// <returns></returns>  
  510.         private JsonObject ParseObject(ref String jsonString)  
  511.         {  
  512.             return new JsonObject(jsonString);  
  513.         }  
  514.   
  515.         /// <summary>  
  516.         /// 定义一个索引器,如果属性是非数组的,返回本身  
  517.         /// </summary>  
  518.         /// <param name="index"></param>  
  519.         /// <returns></returns>  
  520.         public JsonProperty this[int index]  
  521.         {  
  522.             get  
  523.             {  
  524.                 JsonProperty r = null;  
  525.                 if (this._type == JsonPropertyType.Array)  
  526.                 {  
  527.                     if (this._list != null && (this._list.Count - 1) >= index)  
  528.                     {  
  529.                         r = this._list[index];  
  530.                     }  
  531.                 }  
  532.                 else if (index == 0)  
  533.                 {  
  534.                     return this;  
  535.                 }  
  536.                 return r;  
  537.             }  
  538.         }  
  539.   
  540.         /// <summary>  
  541.         /// 提供一个字符串索引,简化对Object属性的访问  
  542.         /// </summary>  
  543.         /// <param name="PropertyName"></param>  
  544.         /// <returns></returns>  
  545.         public JsonProperty this[String PropertyName]  
  546.         {  
  547.             get  
  548.             {  
  549.                 if (this._type == JsonPropertyType.Object)  
  550.                 {  
  551.                     return this._object[PropertyName];  
  552.                 }  
  553.                 else  
  554.                 {  
  555.                     return null;  
  556.                 }  
  557.             }  
  558.             set  
  559.             {  
  560.                 if (this._type == JsonPropertyType.Object)  
  561.                 {  
  562.                     this._object[PropertyName] = value;  
  563.                 }  
  564.                 else  
  565.                 {  
  566.                     throw new NotSupportedException("Json属性不是对象类型!");  
  567.                 }  
  568.             }  
  569.         }  
  570.   
  571.         /// <summary>  
  572.         /// JsonObject值  
  573.         /// </summary>  
  574.         public JsonObject Object  
  575.         {  
  576.             get  
  577.             {  
  578.                 if (this._type == JsonPropertyType.Object)  
  579.                     return this._object;  
  580.                 return null;  
  581.             }  
  582.         }  
  583.   
  584.         /// <summary>  
  585.         /// 字符串值  
  586.         /// </summary>  
  587.         public String Value  
  588.         {  
  589.             get  
  590.             {  
  591.                 if (this._type == JsonPropertyType.String)  
  592.                 {  
  593.                     return this._value;  
  594.                 }  
  595.                 else if (this._type == JsonPropertyType.Number)  
  596.                 {  
  597.                     return this._number.ToString();  
  598.                 }  
  599.                 return null;  
  600.             }  
  601.         }  
  602.   
  603.         public JsonProperty Add(Object value)  
  604.         {  
  605.             if (this._type != JsonPropertyType.Null && this._type != JsonPropertyType.Array)  
  606.             {  
  607.                 throw new NotSupportedException("Json属性不是Array类型,无法添加元素!");  
  608.             }  
  609.             if (this._list == null)  
  610.             {  
  611.                 this._list = new List<JsonProperty>();  
  612.             }  
  613.             JsonProperty jp = new JsonProperty(value);  
  614.             this._list.Add(jp);  
  615.             this._type = JsonPropertyType.Array;  
  616.             return jp;  
  617.         }  
  618.   
  619.         /// <summary>  
  620.         /// Array值,如果属性是非数组的,则封装成只有一个元素的数组  
  621.         /// </summary>  
  622.         public List<JsonProperty> Items  
  623.         {  
  624.             get  
  625.             {  
  626.                 if (this._type == JsonPropertyType.Array)  
  627.                 {  
  628.                     return this._list;  
  629.                 }  
  630.                 else  
  631.                 {  
  632.                     List<JsonProperty> list = new List<JsonProperty>();  
  633.                     list.Add(this);  
  634.                     return list;  
  635.                 }  
  636.             }  
  637.         }  
  638.   
  639.         /// <summary>  
  640.         /// 数值  
  641.         /// </summary>  
  642.         public double Number  
  643.         {  
  644.             get  
  645.             {  
  646.                 if (this._type == JsonPropertyType.Number)  
  647.                 {  
  648.                     return this._number;  
  649.                 }  
  650.                 else  
  651.                 {  
  652.                     return double.NaN;  
  653.                 }  
  654.             }  
  655.         }  
  656.   
  657.         public void Clear()  
  658.         {  
  659.             this._type = JsonPropertyType.Null;  
  660.             this._value = String.Empty;  
  661.             this._object = null;  
  662.             if (this._list != null)  
  663.             {  
  664.                 this._list.Clear();  
  665.                 this._list = null;  
  666.             }  
  667.         }  
  668.   
  669.         public Object GetValue()  
  670.         {  
  671.             if (this._type == JsonPropertyType.String)  
  672.             {  
  673.                 return this._value;  
  674.             }  
  675.             else if (this._type == JsonPropertyType.Object)  
  676.             {  
  677.                 return this._object;  
  678.             }  
  679.             else if (this._type == JsonPropertyType.Array)  
  680.             {  
  681.                 return this._list;  
  682.             }  
  683.             else if (this._type == JsonPropertyType.Bool)  
  684.             {  
  685.                 return this._bool;  
  686.             }  
  687.             else if (this._type == JsonPropertyType.Number)  
  688.             {  
  689.                 return this._number;  
  690.             }  
  691.             else  
  692.             {  
  693.                 return null;  
  694.             }  
  695.         }  
  696.   
  697.         public virtual T GetValue<T>() where T : class  
  698.         {  
  699.             return (GetValue() as T);  
  700.         }  
  701.   
  702.         public virtual void SetValue(Object value)  
  703.         {  
  704.             if (value is String)  
  705.             {  
  706.                 this._type = JsonPropertyType.String;  
  707.                 this._value = (String)value;  
  708.             }  
  709.             else if (value is List<JsonProperty>)  
  710.             {  
  711.                 this._list = ((List<JsonProperty>)value);  
  712.                 this._type = JsonPropertyType.Array;  
  713.             }  
  714.             else if (value is JsonObject)  
  715.             {  
  716.                 this._object = (JsonObject)value;  
  717.                 this._type = JsonPropertyType.Object;  
  718.             }  
  719.             else if (value is bool)  
  720.             {  
  721.                 this._bool = (bool)value;  
  722.                 this._type = JsonPropertyType.Bool;  
  723.             }  
  724.             else if (value == null)  
  725.             {  
  726.                 this._type = JsonPropertyType.Null;  
  727.             }  
  728.             else  
  729.             {  
  730.                 double d = 0;  
  731.                 if (double.TryParse(value.ToString(), out d))  
  732.                 {  
  733.                     this._number = d;  
  734.                     this._type = JsonPropertyType.Number;  
  735.                 }  
  736.                 else  
  737.                 {  
  738.                     throw new ArgumentException("错误的参数类型!");  
  739.                 }  
  740.             }  
  741.         }  
  742.   
  743.         public virtual int Count  
  744.         {  
  745.             get  
  746.             {  
  747.                 int c = 0;  
  748.                 if (this._type == JsonPropertyType.Array)  
  749.                 {  
  750.                     if (this._list != null)  
  751.                     {  
  752.                         c = this._list.Count;  
  753.                     }  
  754.                 }  
  755.                 else  
  756.                 {  
  757.                     c = 1;  
  758.                 }  
  759.                 return c;  
  760.             }  
  761.         }  
  762.   
  763.         public JsonPropertyType Type  
  764.         {  
  765.             get { return this._type; }  
  766.         }  
  767.   
  768.         public override string ToString()  
  769.         {  
  770.             return this.ToString("");  
  771.         }  
  772.   
  773.   
  774.         public virtual string ToString(String format)  
  775.         {  
  776.             StringBuilder sb = new StringBuilder();  
  777.             if (this._type == JsonPropertyType.String)  
  778.             {  
  779.                 sb.Append("'").Append(this._value).Append("'");  
  780.                 return sb.ToString();  
  781.             }  
  782.             else if (this._type == JsonPropertyType.Bool)  
  783.             {  
  784.                 return this._bool ? "true" : "false";  
  785.             }  
  786.             else if (this._type == JsonPropertyType.Number)  
  787.             {  
  788.                 return this._number.ToString();  
  789.             }  
  790.             else if (this._type == JsonPropertyType.Null)  
  791.             {  
  792.                 return "null";  
  793.             }  
  794.             else if (this._type == JsonPropertyType.Object)  
  795.             {  
  796.                 return this._object.ToString();  
  797.             }  
  798.             else  
  799.             {  
  800.                 if (this._list == null || this._list.Count == 0)  
  801.                 {  
  802.                     sb.Append("[]");  
  803.                 }  
  804.                 else  
  805.                 {  
  806.                     sb.Append("[");  
  807.                     if (this._list.Count > 0)  
  808.                     {  
  809.                         foreach (JsonProperty p in this._list)  
  810.                         {  
  811.                             sb.Append(p.ToString());  
  812.                             sb.Append(", ");  
  813.                         }  
  814.                         sb.Length -= 2;  
  815.                     }  
  816.                     sb.Append("]");  
  817.                 }  
  818.                 return sb.ToString();  
  819.             }  
  820.         }  
  821.     }  
  822. }  

 

 

然后是调用地址 和方法

[csharp] view plaincopyprint?
 
  1. public int validateNumber(string mobileNo)  
  2.        {  
  3.            string url = ServiceConfig.serviceUrl["validateNumber"].ToString();  
  4.            string paramsString = "m=" + mobileNo + "&output=json";  
  5.            string result = CommonTools.httpPostByUrl(url, paramsString);  
  6.            if (!result.Equals(""))  
  7.            {  
  8.                JsonObject resultJson = new JsonObject(result);  
  9.                if (resultJson.Properties<string>("QueryResult").Equals("True") && resultJson.Properties<string>("Province").Equals("江西"))  
  10.                {  
  11.                    //成功返回0  
  12.                    return 0;  
  13.                }  
  14.                else if (result.Equals(""))  
  15.                {  
  16.                    return 1;  
  17.                }  
  18.                //失败返回1  
  19.            }  
  20.            return 1;  
  21.        }  


 

 

[csharp] view plaincopyprint?
 
  1. /// <summary>  
  2.       /// 调用httpPost接口  
  3.       /// </summary>  
  4.       /// <param name="url"></param>  
  5.       /// <param name="paramsString"></param>  
  6.       /// <returns></returns>  
  7.       public static string httpPostByUrl(string url, string paramsString)  
  8.       {  
  9.           try  
  10.           {  
  11.               using (var client = new ExtendedWebClient())  
  12.               {  
  13.                   client.Timeout = 3000;  
  14.                   client.Headers.Add("Content-Type""application/x-www-form-urlencoded");  
  15.                   byte[] postData = Encoding.ASCII.GetBytes(paramsString);  
  16.                   byte[] responseData = client.UploadData(url, "POST", postData);  
  17.                   string result = Encoding.UTF8.GetString(responseData);  
  18.                   Console.WriteLine("httpPost result:" + result);  
  19.                   return result;  
  20.               }  
  21.           }  
  22.           catch (Exception ex)  
  23.           {  
  24.               Console.WriteLine(ex);  
  25.               return "";  
  26.           }  
  27.   
  28.       }  


 

 

 

扩展的webCilent

[csharp] view plaincopyprint?
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6.   
  7. namespace QinQinGo.Common  
  8. {  
  9.     public class ExtendedWebClient : WebClient  
  10.     {  
  11.         public int Timeout { getset; }  
  12.   
  13.         protected override WebRequest GetWebRequest(Uri address)  
  14.         {  
  15.             WebRequest request = base.GetWebRequest(address);  
  16.             if (request != null)  
  17.                 request.Timeout = Timeout;  
  18.             return request;  
  19.         }  
  20.   
  21.         public ExtendedWebClient()  
  22.         {  
  23.             Timeout = 100000; // the standard HTTP Request Timeout default  
  24.         }  
  25.     }  
  26.   
  27. }  


 

 

 

最后是接口地址:

http://api.showji.com/Locating/default.aspx

posted @ 2014-04-01 22:34  晕菜一员  阅读(350)  评论(0编辑  收藏  举报