Small component --- express delivery query component, --- express delivery component

Source: Internet
Author: User

Small component --- express delivery query component, --- express delivery component

I recently flipped through the email and found that I copied the. Net component that I used to help the company obtain the express delivery information of the express delivery website during my previous internship. Now let's share it.

I. Research express 100

1. Capture the express address through httpwatch 100.Http://m.kuaidi100.com/query,The API access format isQueryUrl + "? Type = "+ courier company type +" & postid = "+ courier ticket number;

In another blog, I wrote how to capture:A useful network monitoring software

2. The default returned Json string format is:

{"Message": "OK", "status": "1", "state": "3", "data": [{"time": "13:35:14 ", "context": "customer accepted" },{ "time": "09:10:10", "context": "leaving [Beijing Shijingshan Business Office] for delivery, deliveryman [Wen], tel. [] "},{" time ":" 19:46:38 "," context ":" arrive at [Beijing Shijingshan Business Office] "},{" time ":" 15:22:32 ", "context": "When the [Beijing Shijingshan Business Office] is being delivered, the courier [Wen], phone []" },{ "time": "15:05:00", "context ": "arrive at [Beijing Shijingshan Business Office]" },{ "time": "13:37:52", "context ": "Leaving [Beijing _ Same city Transfer Station] to [Beijing Shijingshan Business Office]" },{ "time": "12:54:41", "context ": "arriving at [Beijing _ Same city Transfer Station]" },{ "time": "11:11:03", "context ": "Departure from [Beijing Operation Center Station team] to [Beijing _ Same city Transfer Station]" },{ "time": "10:43:21", "context ": "arrive at [Beijing Operation Center Station team]" },{ "time": "21:18:53", "context ": "Departure from [Fujian _ Xiamen branch] to [Beijing operation center _ airline]" },{ "time": "20:07:27", "context": "received, arrived at [Fujian _ Xiamen branch] "}]}

Ii. Define object classes.
The object class is defined in Json format:

Notes: 1. MQueryParameter query condition; 2. returned results after MResultMsg query; 3. expressageInfo logistics status in a single period of time; 4. errorMsg: records storage access errors, including configuration file errors and web access errors.

Public class MQueryParameter {// <summary> // courier company // </summary> public string TypeCom {get; set ;} /// <summary> /// delivery order No. /// </summary> public string OrderId {get; set ;}} public class MResultMsg {public bool Result {get; set;} public StateType State {get; set;} public string JsonMessage {get; set;} public List <ExpressageInfo> Data {get; set;} public ErrorMsg Error {get; set ;}} public class ExpressageInfo {public DateTime Time {get; set ;}public string Context {get; set ;}} public enum StateType {in-transit, collection, troubleshooting, acceptance, return, dispatch, return} public class ErrorMsg {public string ErrorCode {get; set;} public string ErrorMessage {get; set ;}}

3. configuration file.
For the queried api uri, we should store the configuration file and the Map information of express delivery. It is mainly used to store API changes.

Note: escape characters are used in xml: & (logical and) & amp; <(smaller than) & lt;> (greater than) & gt; "(double quotation marks) & quot; '(single quotes) & apos; [/size]

<Configuration> <etettings> <! -- Mobile api server access address --> <add key = "queryUrl" value = "http://m.kuaidi100.com/query? Type = {0} & amp; postid = {1} "/> <! -- SF Express code --> <add key = "SF Express" value = "shunfeng"/> <add key = "zhongtong Express" value = "zhongtong"/> </ appSettings> </configuration>

Iii. Helper method.
Check the QueryParam parameter and obtain data through the methods provided by Helper. All exceptions in the method must be handled. Components cannot crash the program. Therefore, all exceptions must be handled. Then it is encapsulated in the ErrorMsg object.

Public static MResultMsg GetExpressageMessage (MQueryParameter para) {// obtain the Configuration file Configuration config = null; string queryUrl = string. empty; string com = string. empty; MResultMsg msg = new MResultMsg (); if (string. isNullOrEmpty (para. typeCom) {msg. result = false; msg. error = new ErrorMsg () {ErrorCode = "001", ErrorMessage = "logistics company cannot be blank"}; return msg ;}if (string. isNullOrEmpty (para. orderId) {msg. Result = false; msg. error = new ErrorMsg () {ErrorCode = "002", ErrorMessage = "the order number cannot be blank"}; return msg ;}try {string configPath = System. IO. path. combine (AppDomain. currentDomain. setupInformation. applicationBase, "Expressage. config "); ExeConfigurationFileMap map = new ExeConfigurationFileMap (); map. exeConfigFilename = configPath; config = ConfigurationManager. openMappedExeConfiguration (map, Config UrationUserLevel. None); queryUrl = config. AppSettings. Settings ["queryUrl"] = null? String. Empty: config. etettings. Settings ["queryUrl"]. Value; com = config. AppSettings. Settings [para. TypeCom] = null? String. empty: config. appSettings. settings [para. typeCom]. value;} catch (Exception ex) {msg. result = false; msg. error = new ErrorMsg () {ErrorCode = "003", ErrorMessage = ex. message}; return msg;} if (string. isNullOrEmpty (com) {msg. result = false; msg. error = new ErrorMsg () {ErrorCode = "004", ErrorMessage = "the type of the logistics company for which the configuration file is missing"}; return msg ;} // obtain logistics information online string jsonResult = null; try {queryUrl = string. format (queryUrl, com, para. orderId); WebRequest request = WebRequest. create (@ queryUrl); WebResponse response = request. getResponse (); string message = string. empty; using (Stream stream = response. getResponseStream () {Encoding encode = Encoding. UTF8; using (StreamReader reader = new StreamReader (stream, encode) {message = reader. readToEnd () ;} jsonResult = message ;}} catch (Exception ex) {msg. result = false; msg. error = new ErrorMsg () {ErrorCode = "005", ErrorMessage = ex. message}; return msg;} msg = JSONStringToObj <MResultMsg> (jsonResult); msg. jsonMessage = jsonResult; msg. result = true; return msg;} private static T JSONStringToObj <T> (string JsonStr) {JavaScriptSerializer Serializer = new JavaScriptSerializer (); T objs = Serializer. deserialize <T> (JsonStr); return objs ;}

Iv .:
There is a test data in Frank.Expressage.zip. It may take too long for the Express bill number to expire and no data can be found.

V. Summary

The Code was pasted as it was previously written but not modified. It seems a bit unfriendly, because the Uri is captured by httpwatch. I hope you can go to the official website to apply for an API. The captured uriApi has a limit on the Access frequency. Too frequent access. It will be blocked. Below is the official website address, please support genuine: http://www.kuaidi100.com/openapi/applyapi.shtml

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.