Use flex to access the WebService written by Asp.net.

Source: Internet
Author: User

The process of using the flex program as the client to access the Web Service written with Asp.net is as follows:

1. Use vs to create an Asp.net WebService website, which is named website9. Compile a helloworld method to accept a string type input and a string type output. The source code of this WebService is as follows:

  1. Using system;
  2. Using system. LINQ;
  3. Using system. Web;
  4. Using system. Web. Services;
  5. Using system. Web. Services. Protocols;
  6. Using system. xml. LINQ;
  7. [WebService (namespace = "http://tempuri.org/")]
  8. [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
  9. // To allow this web service to be called from script, using ASP. NET Ajax, uncomment the following line.
  10. // [System. Web. Script. Services. scriptservice]
  11. Public class service: system. Web. Services. WebService
  12. {
  13. Public Service (){
  14. // Uncomment the following line if using designed components
  15. // Initializecomponent ();
  16. }
  17. [Webmethod]
  18. Public String helloworld (string input ){
  19. Return "Hello World," + input;
  20. }
  21. }

2. Specify a fixed port for this website project, which is assumed to be 8888. Run website9 and you can see that the WSDL address of this WebService is: http: // localhost: 8888/website9/service. asmx? WSDL
Open this address and find the following information, indicating that the input parameter accepted by the helloworld function is input, and the returned parameter is helloworldresult.

  1. <S: element name = "helloworld">
  2. <S: complextype>
  3. <S: sequence>
  4. <S: Element minoccurs = "0" maxoccurs = "1" name = "input" type = "s: string"/>
  5. </S: sequence>
  6. </S: complextype>
  7. </S: Element>
  8. <S: element name = "helloworldresponse">
  9. <S: complextype>
  10. <S: sequence>
  11. <S: Element minoccurs = "0" maxoccurs = "1" name = "helloworldresult" type = "s: string"/>
  12. </S: sequence>
  13. </S: complextype>
  14. </S: Element>

3. Compile a flex program to access the preceding WebService. You can use mxml or ActionScript in either of the following ways.

Method 1: Use mxml

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" fontsize = "12">
  3. <Mx: SCRIPT>
  4. <! [CDATA [
  5. Import MX. rpc. Events. resultevent;
  6. Import MX. rpc. Events. faultevent;
  7. Private var WS: WebService = new WebService ();
  8. Internal function sendmessage (): void {
  9. Loader. helloworld. Send ();
  10. }
  11. Internal function completehandler (EVT: resultevent): void {
  12. Result_txt.text = EVT. Result. tostring ();
  13. }
  14. Internal function errorhandler (EVT: faultevent): void {
  15. Result_txt.text = EVT. fault. tostring ();
  16. }
  17. ]>
  18. </MX: SCRIPT>
  19. <Mx: WebService id = "loader" WSDL = "http: // localhost: 8888/website9/service. asmx? WSDL "result =" completehandler (event) "showbusycursor =" true "fault =" errorhandler (event) ">
  20. <Mx: Operation name = "helloworld">
  21. <Mx: Request>
  22. <Input> <! -- Input parameter name: Input -->
  23. {Input_txt.text}
  24. </Input>
  25. </MX: Request>
  26. </MX: Operation>
  27. </MX: WebService>
  28. <Mx: textinput x = "110" Y = "134" width = "249" id = "input_txt"/>
  29. <Mx: button x = "376" Y = "134" label = "Send request" id = "send_btn" Click = "sendmessage ()"/>
  30. <Mx: textarea x = "110" Y = "193" width = "344" Height = "258" id = "result_txt"/>
  31. </MX: Application>

Method 2: use ActionScript

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" fontsize = "12">
  3. <Mx: SCRIPT>
  4. <! [CDATA [
  5. Import MX. rpc. Events. resultevent;
  6. Import MX. rpc. Events. faultevent;
  7. Import MX. rpc. Soap. WebService; // Add a reference to the WebService Control
  8. Private var WS: WebService = new WebService ();
  9. Internal function sendmessage (): void {
  10. Loader. helloworld. Send ();
  11. WS. helloworld. addeventlistener ("result", completehandler );
  12. WS. helloworld. addeventlistener ("fault", errorhandler );
  13. WS. loadwsdl ("http: // localhost: 8888/website9/service. asmx? WSDL ");
  14. VaR STR: String = input_txt.text;
  15. WS. helloworld (STR); // call the remote function directly.
  16. }
  17. Internal function completehandler (EVT: resultevent): void {
  18. Result_txt.text = EVT. Result. tostring ();
  19. }
  20. Internal function errorhandler (EVT: faultevent): void {
  21. Result_txt.text = EVT. fault. tostring ();
  22. }
  23. ]>

  24. </MX: SCRIPT>
  25. </MX: WebService>
  26. <Mx: textinput x = "110" Y = "134" width = "249" id = "input_txt"/>
  27. <Mx: button x = "376" Y = "134" label = "Send request" id = "send_btn" Click = "sendmessage ()"/>
  28. <Mx: textarea x = "110" Y = "193" width = "344" Height = "258" id = "result_txt"/>
  29. </MX: Application>

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.