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:
- <〈! -- Operationresponseelement --> --〉
- <Elementname = "result"> "〉
- <Complextype> 〉
- <〈Sequence> 〉
- <Elementname = "A" type = "XSD: int"
- Minoccurs = "1" maxoccurs = "1"/> "/〉
- <Elementname = "B" type = "XSD: int"
- Minoccurs = "1" maxoccurs = "1"/> "/〉
- <〈/Sequence> 〉
- </Complextype> 〉
- </Element> 〉
- The above is generated by gsoap. Returns the object result,
- An object has two attributes: A and B.
- <S: elementname = "testresponse"> "〉
- <S: complextype> 〉
- <S:Sequence> 〉
- <S: elementminoccurs = "0" maxoccurs = "1"
- Name= "Testresult" type = "TNS: result"/> "/〉
- </S:Sequence> 〉
- </S: complextype> 〉
- </S: Element> 〉
- <S: complextypename = "result"> "〉
- <S:Sequence> 〉
- <S: elementminoccurs = "1" maxoccurs = "1"
- Name= "A" type = "s: int"/> "/〉
- <S: elementminoccurs = "1" maxoccurs = "1"
- Name= "B" type = "s: int"/> "/〉
- </S:Sequence> 〉
- </S: complextype> 〉
- The above is generated by. net.
-
- In the following file
- <S: elementname = "testresponse"> "〉
- <S: complextype> 〉
- <S:Sequence> 〉
- <S: elementminoccurs = "0" maxoccurs = "1"
- Name= "Testresult" type = "TNS: result"/> "/〉
- </S:Sequence> 〉
- </S: complextype> 〉
- </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:
- /// <Summary> 〉
- /// Convert from hexadecimal to Chinese Characters
- /// </Summary> 〉
- /// <Paramname = "hex"> </param> 〉
- /// <///〈Returns> <〉〈/Returns> 〉
- Publicstaticstringgetchsfromhex (stringhex)
- {
- If (hex = NULL)
- Thrownewargumentnullexception ("hex ");
- If (Hex. Length % 2! = 0)
- {
- Hex + = "20"; // Space
- // Thrownewargumentexception
- ("Hexisnotavalidnumber! "," Hex ");
- }
- // You Need to Convert hex to a byte array.
- Byte [] bytes = newbyte [Hex. Length/2];
-
- For(INTI = 0; I <bytes. length; I ++)
- {
- Try
- {
- // Each two characters is a byte.
- Bytes [I] = byte. parse (Hex. substring (I * 2, 2 ),
- System. Globalization. numberstyles. hexnumber );
- }
- Catch
- {
- // Rethrowanexceptionwithcustommessage.
- Thrownewargumentexception ("
- Hexisnotavalidhexnumber! "," Hex ");
- }
- }
-
- // Obtain gb2312 and chinesesimplified.
- System. Text. encodingchs = system. Text. encoding.
- Getencoding ("gb2312 ");
- Returnchs. getstring (bytes );
- }
-
- /// <Summary> 〉
- /// Convert Chinese characters to hexadecimal
- /// </Summary> 〉
- /// <Paramname = "S"> </param> 〉
- /// <///〈Returns> <〉〈/Returns> 〉
- Publicstaticstringgethexfromchs (strings)
- {
- If (S. Length % 2 )! = 0)
- {
- S + = ""; // Space
- // Thrownewargumentexception ("
- Sisnotvalidchinesestring! ");
- }
-
- System. Text. encodingchs = system. Text.
- Encoding. getencoding ("gb2312 ");
-
- Byte [] bytes = CHS. getbytes (s );
- Stringstr = "";
- For(INTI = 0; I <bytes. length; I ++)
- {
- STR + = string. Format ("{0: x}", bytes [I]);
- }
- Returnstr;
- }
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