Accessing the data query service of the electronic Hub Credit Center with. Net

Source: Internet
Author: User

Brief description

Electronic hub full name National Transportation Logistics Public Information platform, mainly to provide logistics and production enterprises for logistics related data exchange standards and APIs, detailed introduction can refer to its official website www.logink.org, this article assumes that the reader has a knowledge of the platform, and has successfully applied for the corresponding account and data exchange services.

The credit Center is one of the many data services of the electronic hub, which provides the uploading and inquiring of the credit information of the logistics participants, including transportation vehicles, practitioners, etc. The official examples and presentations are mostly Java-based, with very few. NET, and hopefully this article will help. NET developers quickly master the way data is exchanged.

The data service of the electronic hub is divided into two kinds, one is called data exchange, the other is called service invocation.

data Exchange is an e-mail-like behavior, the electronic hub can be seen as a mail server, sending and receiving data is similar to the way e-mail.

Service Invocation is a regular HTTP request, mainly used to query the electronic hub information, for example, the credit center, can query the traffic information of the vehicle, integrity records and so on.

This article mainly introduces the service invocation mode, to invoke the service provided by the electronic hub, first of all must ensure that the relevant services have been opened, after the opening also need to obtain the service ID, these tasks can be completed from the Electronic hub User Management Center.

Before invoking a service, you must obtain a user-authenticated token (access token), which can be done through the Unified Authentication Service, and after the token has been obtained, the service can be invoked on the token.

Introducing Services

The data interface of the electronic hub is provided by the Web service, so before you start coding, you can first introduce the relevant service, add the service reference directly in VS, and the related service address can be found in the open Access Center of the official website.

It is important to note that while most of the Web services provided by electronic hubs can be introduced directly in VS, there are other ways in which this "Add Service Reference" cannot be used, and credit center services are now known. For this scenario, you need to add a Web reference in VS, as follows.

Right-click on the project or reference (Reference) node in the Solution browser, select "Add Service Reference" in the menu, click "Advanced" in the Pop-up dialog and click on the Service Reference Settings dialog box. Add Web Reference to bring up a dialog box that adds a Web reference.

As for why, because I do not know much about soap and WCF, I cannot answer this question, and I hope the great God in this respect can answer it.

Access code

When the reference is added, VS will automatically generate the appropriate type for us, and we can use it directly.

The first is to get the token, the client type of the Unified authentication Service is authenserviceclient, and after instantiation calls his authenticate method, the method signature is as follows:

Authenticate (string applicant, string userid, string password, string resource)

For a specific explanation of this method, refer to the official note.

The Tokenvalid property of the return value indicates whether the validation was successful, and if true, the token value can be obtained through the token property. The sample code is as follows:

1 Private BOOLAuthenticate (Loginkuser user,stringResId, out stringtoken)2 {3Authenserviceclient clnt =Newauthenserviceclient ();4 5     varresult =clnt.authenticate (user. Exchangecode, user. Exchangecode, user. Password, resId);6 7     if(result.tokenvalied)8token =Result.token;9     ElseTentoken =NULL; One  A clnt. Close (); -  -     returnToken! =NULL; the}

After the token is obtained, the query service of the credit Center can be called directly, and the client type of the generated Credit Center service is Loginkserviceservice, which invokes its InterfaceName method after instantiation.

Genericresult InterfaceName (authentication authentication, publicinformation publicinformation, string Businessinformation)

This method did not find the official documents, due to the complexity of the various parameters, do not introduce (in fact, I do not understand), directly copy the following example can be.

1 Private stringCallcreditservice (loginkuser user,stringTokenstringActionstringrequest)2 {3     varCSS =_settings. Creditservice;4 5Logink.Services.Credit.security Security =NewLogink.Services.Credit.security ();6Security. Logisticsexchangecode =user. Exchangecode;7Security. Usertokenid =token;8 9Logink.Services.Credit.authentication authentication =NewLogink.Services.Credit.authentication ();TenAuthentication. UserName =user. Exchangecode; OneAuthentication. UserPassword =user. Password; AAuthentication. ServiceId =CSS. ResourceId; -Authentication. UserId =user. Exchangecode; -  theLogink.Services.Credit.publicInformation publicinformation =NewLogink.Services.Credit.publicInformation (); -Publicinformation.servicetype ="3"; -Publicinformation.actiontype =Action; -  +Logink.Services.Credit.LoginkServiceService Service =NewLogink.Services.Credit.LoginkServiceService (); -Service. URL =CSS. URL; +Service. Security =security; A  at     varresult =service. InterfaceName (authentication, publicinformation, request); -  -     if(result.) ResultCode) -     { -         returnresult. businessinformation; -     } in     Else -         Throw Newexchangeexception (result. Exceptioninformationcode, result. exceptioninformation); to}

In the example, this Callcreditservice method has encapsulated various parameters that call the Credit Center service, where the action parameter represents the business type, the official website is introduced, and the following details the request parameter.

The request parameter is actually a BASE64 encoded XML string, the content of the XML is the value of each parameter passed in, it should be noted that the incoming XML string is not a complete document, but the root node below the content, do not put the root node also passed up, If you use XmlDocument to handle incoming parameters, you can use the InnerXml property of the root node.

BASE64 encoding is relatively simple, the System.Convert type directly support conversion to BASE64, by default, the electronic hub uses UTF8 encoding, do not make mistakes in/decode, otherwise you will not find data or garbled.

 1  private  string  XmlToBase64 (string   2  { 3  if  (string  . IsNullOrEmpty (XML))  4  return  Span style= "color: #000000;" > XML;  5   return   Convert.tobase64string (Encoding.Utf8.GetBytes (XML));  7 } 

Finally, you need to handle the return value of InterfaceName, the return value of the ResultCode indicates whether the call succeeds, and if it is not successful, the error code can be obtained through the Exceptioninformationcode property. Otherwise, the returned text can be obtained through the Businessinformation property.

The returned text is also a BASE64 encoded XML string, converted into clear text can be used directly, but for the processing of clear text is a relatively headache, after many experiments finally found the following rules.

The text is an empty string: There may be no query to the relevant content.

The text is not XML: Although ResultCode is true, it may still be an error, and the content of the text is the error message.

In these cases, our procedures should be handled accordingly.

1  PublicXmlDocument querycredit (queryparameters parameters)2 {3     if(Parameters = =NULL)4         Throw NewArgumentNullException (nameof (parameters));5 6 Accesstoken token;7     stringdata;8     BOOLForcerenewtoken =false;9 Ten Retry: One  A     //obtaining a user's access token through the Unified Authentication Service -token =GetToken (_user, _settings. Creditservice.resourceid, Forcerenewtoken); -  the     //serializing incoming parameters to XML -data =parameters. GETXML (); -     //convert XML to BASE64 encoding -data =XmlToBase64 (data); +  -     Try +     { A         //call the Web Service of the credit center atdata =Callcreditservice (_user, token. Value, parameters. ActionName, data); -     } -     Catch(exchangeexception ee) -     { -         //determine if a token needs to be updated -         if(EE. Istokeninvalid &&!Forcerenewtoken) in         { -Forcerenewtoken =true; to             Gotoretry; +         } -         Else the             Throw; *     } $ Panax Notoginseng     if(!string. IsNullOrEmpty (data)) -     { the         //The returned content has been BASE64 encoded and converted to XML.  +data =base64toxml (data); A  the         //In some cases, the platform may return a string of error messages instead of XML, so the error message is processed directly at the beginning of the "<".  +         if(data[0] =='<') -         { $XmlDocument doc =NewXmlDocument (); $ Doc. LOADXML (data); -  -             returnDoc; the         } -         ElseWuyi             Throw Newexchangeexception (data); the     } -     Else Wu     { -         //If the returned content is empty, the relevant data may not be found.  About         Throw NewExchangeexception ("110008","No data has been found for the time being"); $     } -  -}

So far, the code required to call the Credit Center service has been roughly introduced, strung together to form a complete program, all source code click here. Because my computer is VS2015, the lower version of VS may not be able to open, I am sorry.

Related Resources

Several QQ groups that must be added

213604083: Platform Access Group, this is a must add a group, there are many platform managers in the inside, some basic questions can ask them, of course, it is also important that the group has many resources to develop.

383412768: Credit Access group, if you want to use Credit center services, this is the organization you are looking for.

363016382: Campus Access Group, if you are to access the access (you understand), then this is also necessary.

601484722: Fill the data access group.

Accessing the data query service of the electronic Hub Credit Center with. Net

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.