码迷,mamicode.com
首页 > 其他好文 > 详细

C#快递单号查询源码

时间:2014-06-01 12:25:54      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   tar   

源码本人测试过,没有啥问题,能查询快递单号,支持的快递还挺多,圆通快递、申通快递、韵达快递的都支持单号查询的,程序是通过向爱快递(www.aikuaidi.cn)接口传输参数来查询快递单号,我直接把代码帖出来,很好的解决我单个开发的麻烦。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/// <summary>
        /// 同步单号查询方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="order"></param>
        /// <param name="isSign"></param>
        /// <param name="isLast"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T APIQueryDataSYNC<T>(string id,
                                             string order,
                                             bool isSign,
                                             bool isLast,
                                             T defaultValue)
        {
            try
            {
                string currTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string currKey = key;
                if (isSign)
                {
                    currKey = Utils.GetSign(uid, key, id, order, currTime);
                    currKey += "&issign=true";
                }
 
                string url = sync_url + string.Format("?uid={0}&key={1}&id={2}&order={3}&time={4}",
                                            uid, currKey, id, order, HttpUtility.UrlEncode(currTime));
 
                string html = Utils.GET_WebRequestHTML("utf-8", url);
 
                if (!string.IsNullOrEmpty(html))
                    return Utils.JsonToObj<T>(html, defaultValue);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
 
            return defaultValue;
        }
 
    }
 
    /// <summary>
    /// 辅助工具类
    /// </summary>
    public class Utils
    {
        public static string GetSign(int uid, string key, string id, string order, string time)
        {
            string sign = string.Format("uid={0}&key={1}&id={2}&order={3}&time={4}",
                                        uid,
                                        key,
                                        id,
                                        HttpUtility.UrlEncode(order.ToLower()),
                                        HttpUtility.UrlEncode(time));
 
            return Md5Encrypt(sign.ToLower(), "utf-8");
        }
 
        public static string Md5Encrypt(string strToBeEncrypt, string encodingName)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            Byte[] FromData = System.Text.Encoding.GetEncoding(encodingName).GetBytes(strToBeEncrypt);
            Byte[] TargetData = md5.ComputeHash(FromData);
            string Byte2String = "";
            for (int i = 0; i < TargetData.Length; i++)
            {
                Byte2String += TargetData[i].ToString("x2");
            }
            return Byte2String;
        }
 
        public static T GetRequest<T>(string key, T defaultValue)
        {
            string value = HttpContext.Current.Request[key];
 
            if (string.IsNullOrEmpty(value))
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return (T)Convert.ChangeType(value, typeof(T));
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
 
        public static T GetAppConfig<T>(string key, T defaultValue)
        {
            string value = ConfigurationManager.AppSettings[key];
 
            if (string.IsNullOrEmpty(value))
            {
                return defaultValue;
            }
            else
            {
                try
                {
                    return (T)Convert.ChangeType(value, typeof(T));
                }
                catch
                {
                    return defaultValue;
                }
            }
        }
 
        public static string ObjToJson<T>(T data)
        {
            try
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(data.GetType());
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, data);
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            catch
            {
                return null;
            }
        }
 
        public static T JsonToObj<T>(string json, T defaultValue)
        {
            try
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                {
                    object obj = serializer.ReadObject(ms);
 
                    return (T)Convert.ChangeType(obj, typeof(T));
                }
            }
            catch
            {
                return defaultValue;
            }
        }
 
        public static T XmlToObj<T>(string xml, T defaultValue)
        {
            try
            {
                System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
                {
                    object obj = serializer.ReadObject(ms);
 
                    return (T)Convert.ChangeType(obj, typeof(T));
                }
            }
            catch
            {
                return defaultValue;
            }
        }
 
        public static string ObjToXml<T>(T data)
        {
            System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, data);
                return Encoding.UTF8.GetString(ms.ToArray());
 
            }
        }
 
        public static string GET_WebRequestHTML(string encodingName, string htmlUrl)
        {
            string html = string.Empty;
 
            try
            {
                Encoding encoding = Encoding.GetEncoding(encodingName);
 
                WebRequest webRequest = WebRequest.Create(htmlUrl);
                HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
 
                html = streamReader.ReadToEnd();
 
                httpWebResponse.Close();
                responseStream.Close();
                streamReader.Close();
            }
            catch (WebException ex)
            {
                throw new Exception(ex.Message);
            }
 
            return html;
        }
}

  

C#快递单号查询源码,布布扣,bubuko.com

C#快递单号查询源码

标签:c   class   blog   code   a   tar   

原文地址:http://www.cnblogs.com/zhangjin001/p/3762937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!