[Reprinted] C # three major problems of WebService calling

Source: Internet
Author: User

1. Using C # To implement WebService is quite simple. We only need to create a web service program, add [webmethod] to the method name, and deploy it on IIS, you can access WebService as you visit a Web site.

When writing a client in C #, you only need to add the WebService to the reference, so that you can call the C # WebService just like calling a local method. Examples like this are also everywhere, so I will not talk about them here.

2. Use C ++ to implement WebService, and generally use gsoap. For details, see: http://www.cppblog.com/qiujian5628/archive/2008/06/19/54019.html.

3. After these steps are completed, it does not mean that the WebService can communicate with each other. Now I will briefly list the following issues:

1. the URL of the WebService provided by C # is generally like http: // localhost/WebService. asmx, however, C ++ can only provide: http: // localhost /. C ++ can be called when it is used as the client. However, when C # is used as the client and references The RUL provided by C ++, the system will prompt that no execution method is used (http get method not implemented ). Most of the C # developers will think that the C ++ side does not provide the WebService, or the WebService provided is completely incomplete, without the. asmx file. When C ++ is developed, it will think that the data transmitted by him complies with the SOAP protocol and transmits data through HTTP. He is a WebService.

2. When we solve the first step, we will find another problem. When we need to transmit Custom Data Types (called struct in C ++ and called entity in C #), from the information returned by C ++, C # unable to build entity classes.

3. When the transmitted information contains Chinese characters, garbled characters are everywhere.

4. To solve these problems, let's take a brief look at WebService.

Web Service Interoperability Protocol Stack:

<A> service discovery (UDDI)

<B>, service description (WSDL)

<C>, service calling (SOAP)

<D>, message encoding (XML)

<E>, transmission network layer (HTTP, TCP/IP)

Specifically, WSDL describes the methods, parameters, and return values of WebService. Soap (Simple Object Access Protocol) is a lightweight, simple, XML-based protocol. The transmitted data must follow this protocol. I simply think that the transmitted data must follow this format.

The following figure is used to describe the WebService call process:

5. Start solving the problem. As A. Net developer, we don't have access to the underlying things and are encapsulated.

C ++ is indeed a WebService, but they need to provide a description document, that is, the. WSDL file. Use the wsdl.exe tool of .netbench and run the command: WSDL/O: C:/WebService. cs c:/WebService. WSDL. Use the WebService. WSDL document to generate a proxy class and write the proxy class to the WebService. CS file. Copy the CS file to the project and point the URL to http: // localhost/to use WebService as before.

When complex types of data cannot be transmitted, it is because the WSDL file generated using gsoap is different from the WSDL file generated in. net. The Code is as follows:

 
 
  1. <〈! -- Operationresponseelement --> --〉
  2. <Elementname = "result"> "〉
  3. <Complextype> 〉
  4. <〈Sequence> 〉
  5. <Elementname = "A" type = "XSD: int"
  6. Minoccurs = "1" maxoccurs = "1"/> "/〉
  7. <Elementname = "B" type = "XSD: int"
  8. Minoccurs = "1" maxoccurs = "1"/> "/〉
  9. <〈/Sequence> 〉
  10. </Complextype> 〉
  11. </Element> 〉
  12. The above is generated by gsoap. Returns the object result,
  13. An object has two attributes: A and B.
  14. <S: elementname = "testresponse"> "〉
  15. <S: complextype> 〉
  16. <S:Sequence> 〉
  17. <S: elementminoccurs = "0" maxoccurs = "1"
  18. Name= "Testresult" type = "TNS: result"/> "/〉
  19. </S:Sequence> 〉
  20. </S: complextype> 〉
  21. </S: Element> 〉
  22. <S: complextypename = "result"> "〉
  23. <S:Sequence> 〉
  24. <S: elementminoccurs = "1" maxoccurs = "1"
  25. Name= "A" type = "s: int"/> "/〉
  26. <S: elementminoccurs = "1" maxoccurs = "1"
  27. Name= "B" type = "s: int"/> "/〉
  28. </S:Sequence> 〉
  29. </S: complextype> 〉
  30. The above is generated by. net.
  31. In the following file
  32. <S: elementname = "testresponse"> "〉
  33. <S: complextype> 〉
  34. <S:Sequence> 〉
  35. <S: elementminoccurs = "0" maxoccurs = "1"
  36. Name= "Testresult" type = "TNS: result"/> "/〉
  37. </S:Sequence> 〉
  38. </S: complextype> 〉
  39. </S: Element> 〉

This is used in. Net to construct entities. In case of 4.2, gsoap tries its best to use the. NET-generated WSDL document to generate a. h file, so that the structure in C ++ cannot be converted into an entity in C.

The third problem is that we convert Chinese to hexadecimal and then convert it to Chinese. The following code provides C # conversion:

 
 
  1. /// <Summary> 〉
  2. /// Convert from hexadecimal to Chinese Characters
  3. /// </Summary> 〉
  4. /// <Paramname = "hex"> </param> 〉
  5. /// <///〈Returns> <〉〈/Returns> 〉
  6. Publicstaticstringgetchsfromhex (stringhex)
  7. {
  8. If (hex = NULL)
  9. Thrownewargumentnullexception ("hex ");
  10. If (Hex. Length % 2! = 0)
  11. {
  12. Hex + = "20"; // Space
  13. // Thrownewargumentexception
  14. ("Hexisnotavalidnumber! "," Hex ");
  15. }
  16. // You Need to Convert hex to a byte array.
  17. Byte [] bytes = newbyte [Hex. Length/2];
  18. For(INTI = 0; I <bytes. length; I ++)
  19. {
  20. Try
  21. {
  22. // Each two characters is a byte.
  23. Bytes [I] = byte. parse (Hex. substring (I * 2, 2 ),
  24. System. Globalization. numberstyles. hexnumber );
  25. }
  26. Catch
  27. {
  28. // Rethrowanexceptionwithcustommessage.
  29. Thrownewargumentexception ("
  30. Hexisnotavalidhexnumber! "," Hex ");
  31. }
  32. }
  33. // Obtain gb2312 and chinesesimplified.
  34. System. Text. encodingchs = system. Text. encoding.
  35. Getencoding ("gb2312 ");
  36. Returnchs. getstring (bytes );
  37. }
  38. /// <Summary> 〉
  39. /// Convert Chinese characters to hexadecimal
  40. /// </Summary> 〉
  41. /// <Paramname = "S"> </param> 〉
  42. /// <///〈Returns> <〉〈/Returns> 〉
  43. Publicstaticstringgethexfromchs (strings)
  44. {
  45. If (S. Length % 2 )! = 0)
  46. {
  47. S + = ""; // Space
  48. // Thrownewargumentexception ("
  49. Sisnotvalidchinesestring! ");
  50. }
  51. System. Text. encodingchs = system. Text.
  52. Encoding. getencoding ("gb2312 ");
  53. Byte [] bytes = CHS. getbytes (s );
  54. Stringstr = "";
  55. For(INTI = 0; I <bytes. length; I ++)
  56. {
  57. STR + = string. Format ("{0: x}", bytes [I]);
  58. }
  59. Returnstr;
  60. }

Note: The above conversion code is from the network. The code to be converted in C ++ can also be found on the Internet. After the above steps, C ++ and C # WebService can be basically implemented.

At this end, the three major problems are actually the greatest problems in the process, that is, communication between people. Because one party uses C ++ and the other party uses C #, the language is different, and the methods of thinking about the problem are different, they need to understand each other and think about the problem from the perspective of the other party. Multi-communication and multi-communication are the solutions to the problem. Do not complain about C # being mentally retarded. Do not blame C ++ for its complexity. If a language already exists, it has its own value.

Link: http://hi.baidu.com/luzhichen/blog/item/8c0aea72f9dfac118601b066.html

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.