Use. net to access the data query service of the e-hub credit center, and use. net to query data

Source: Internet
Author: User

Use. net to access the data query service of the e-hub credit center, and use. net to query data
Summary

The electronic hub is the National Public Information Platform for transportation and logistics. It provides standards and APIs for logistics-related data exchange between logistics and production enterprises. For details, refer to its official website www.logink.org, this document assumes that the reader has an understanding of the platform and has successfully applied for an account and data exchange service.

The credit center is one of the many data services of the electronic hub. It provides the upload and query of credit information of logistics participants, including transportation vehicles and practitioners. The official examples and introductions are mostly based on Java, and. net is rarely used. I hope this article will help. net developers quickly master the data exchange methods.

The data service of the electronic hub is divided into two types: data exchange and service call.

Data ExchangeIt is an email-like action. You can regard the e-hub as an email server. sending and receiving data is similar to sending and receiving emails.

Service callIt is a conventional HTTP request, which is mainly used to query information from the electronic hub. For example, for the credit center, the vehicle transportation and administration information and integrity records can be queried.

This article mainly introduces the service calling method. To call the services provided by the e-hub, you must first make sure that the relevant services have been activated and then obtain the service ID, all these tasks can be completed from the user management center of the e-hub.

Before calling a service, you must obtain the user-verified Token. This work can be completed through the unified authentication service. After obtaining the Token, you can call the relevant service with the Token.

Introduce services

Data interfaces of the electronic hub are provided by Web services. Therefore, before coding, you can introduce related services and directly add Service references to, the service address can be found in the open access center on the official website.

It is worth noting that, although most of the Web services provided by the e-hub can be directly introduced in VS, some of them cannot use this "add Service reference" method, currently, the credit center service is known. In this case, you need to add a Web reference in VS. The specific method is as follows.

Right-click the project or Reference node in the solution browser, and select "Add Service Reference" from the menu ), click "Advanced" in the pop-up dialog box, and then click "Add Web Reference" in the pop-up service Reference Settings dialog box) the add Web reference dialog box is displayed.


I don't know much about SOAP and WCF, so I can't answer this question.

Access code

After the reference is added, VS will automatically generate the corresponding type for us and use it directly.

The first step is to obtain the token. The client type of the unified authentication service is AuthenServiceClient. after instantiation, The authenticate method is called. The method signature is as follows:

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

For details about this method, refer to the official instructions.

The tokenValid attribute of the returned value indicates whether the verification is successful. If it is true, you can use the token attribute to obtain the value of the token. The sample code is as follows:

 1 private bool Authenticate(LoginkUser user, string resId, out string token)
 2 {
 3     AuthenServiceClient clnt = new AuthenServiceClient();
 4 
 5     var result = clnt.authenticate(user.ExchangeCode, user.ExchangeCode, user.Password, resId);
 6 
 7     if (result.tokenValied)
 8         token = result.token;
 9     else
10         token = null;
11 
12     clnt.Close();
13 
14     return token != null;
15 }

After obtaining the Token, you can directly call the query service of the credit center. The client type of the credit center service generated by VS is LoginkServiceService. after instantiation, call its InterfaceName method.

GenericResult InterfaceName (authentication Authentication, publicInformation PublicInformation, string BusinessInformation)

This method has not been found in the official documentation. Because the parameters are complex, I will not introduce them one by one (I did not understand it myself). just copy the example below.

 1 private string CallCreditService(LoginkUser user, string token, string action, string request)
 2 {
 3     var css = _settings.CreditService;
 4 
 5     Logink.Services.Credit.security security = new Logink.Services.Credit.security();
 6     security.LogisticsExchangeCode = user.ExchangeCode;
 7     security.UserTokenID = token;
 8 
 9     Logink.Services.Credit.authentication authentication = new Logink.Services.Credit.authentication();
10     authentication.UserName = user.ExchangeCode;
11     authentication.UserPassword = user.Password;
12     authentication.ServiceId = css.ResourceId;
13     authentication.UserId = user.ExchangeCode;
14 
15     Logink.Services.Credit.publicInformation publicInformation = new Logink.Services.Credit.publicInformation();
16     publicInformation.ServiceType = "3";
17     publicInformation.ActionType = action;
18 
19     Logink.Services.Credit.LoginkServiceService service = new Logink.Services.Credit.LoginkServiceService();
20     service.Url = css.Url;
21     service.Security = security;
22 
23     var result = service.InterfaceName(authentication, publicInformation, request);
24 
25     if (result.ResultCode)
26     {
27         return result.BusinessInformation;
28     }
29     else
30         throw new ExchangeException(result.ExceptionInformationCode, result.ExceptionInformation);
31 }

In this example, the CallCreditService method has encapsulated various parameters that call the credit center service. The action parameter indicates the service type. The official website introduces the parameters. The following describes the request Parameters in detail.

The request parameter is actually a Base64 encoded XML string. The XML content is the values of each input parameter. Note that the imported XML string is not a complete document, instead of passing the content below the root node. If you use XmlDocument to process incoming parameters, you can use the InnerXml attribute of the root node.

Base64 encoding is relatively simple, System. the Convert type can be directly converted to Base64. By default, the electronic hub uses UTF8 encoding. Do not make a mistake during encoding/decoding. Otherwise, no data or garbled characters will be found.

1 private string XmlToBase64(string xml)
2 {
3     if (string.IsNullOrEmpty(xml))
4         return xml;
5 
6     return Convert.ToBase64String(Encoding.Utf8.GetBytes(xml));
7 }

Finally, the returned value of InterfaceName must be processed. The ResultCode of the returned value indicates whether the call is successful. If the call is unsuccessful, the error code can be obtained through the predictionionioncode attribute. Otherwise, the returned text can be obtained through the BusinessInformation attribute.

The returned text is also a Base64 encoded XML string, which can be directly used after being converted to plain text. However, plaintext processing is a headache, after many tests, the following rules are finally found out.

The text is a blank string: The relevant content may not be found.

Text is not XML: Although ResultCode is true, an error may still occur, and the content of the text is the error message.

In these cases, our programs should be processed accordingly.

1 public XmlDocument QueryCredit (QueryParameters parameters)
 2 {
 3 if (parameters == null)
 4 throw new ArgumentNullException (nameof (parameters));
 5
 6 AccessToken token;
 7 string data;
 8 bool forceRenewToken = false;
 9 
10 retry:
11
12 // Get user's access token through unified authentication service
13 token = GetToken (_user, _settings.CreditService.ResourceId, forceRenewToken);
14
15 // Serialize incoming parameters to XML
16 data = parameters.GetXml ();
17 // Convert XML to BASE64 encoding
18 data = XmlToBase64 (data);
19
20 try
twenty one     {
22 // Call the Web Service of the Credit Center
23 data = CallCreditService (_user, token.Value, parameters.ActionName, data);
twenty four     }
25 catch (ExchangeException ee)
26 {
27 // determine if the token needs to be updated
28 if (ee.IsTokenInvalid &&! ForceRenewToken)
29 {
30 forceRenewToken = true;
31 goto retry;
32}
33 else
34 throw;
35}
36
37 if (! String.IsNullOrEmpty (data))
38 {
39 // The returned content has been BASE64 encoded and converted to XML.
40 data = Base64ToXml (data);
41
42 // In some cases, the platform may return a string of error information instead of XML, so the error message is processed directly if it does not start with "<".
43 if (data [0] == '<')
44 {
45 XmlDocument doc = new XmlDocument ();
46 doc.LoadXml (data);
47
48 return doc;
49}
50 else
51 throw new ExchangeException (data);
52}
53 else
54 {
55 // If the returned content is empty, the related data may not be found.
56 throw new ExchangeException ("110008", "The corresponding data was not found temporarily");
57}
58
59} 

So far, the Code required to call the credit center service has been roughly introduced, and a complete program has been formed after they are concatenated. Click here for all the source code. Because my computer is equipped with 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 mandatory group. There are many platform managers in it. You can ask them some basic questions, of course, it is very important that the Group has many resources required for development.

383412768: credit access group. If you want to use the credit center service, this is the organization you are looking.

363016382: the campus access group. If you want to access the community (You know), this is also necessary.

601484722: Fill in the Data Access Group.


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.