C # WebService call example

Source: Internet
Author: User

C # There are many similarities between WebService calling and JS calling. Let's implement it step by step:

Step 1: Create a WebService

This step is the same as calling WebService in JS. For details, visit the following address:

Address: http://www.cnblogs.com/puresoul/archive/2010/08/19/1803567.html

Step 2: Create a page to implement C # Call Web Services

Add a button on the page. The background code is as follows:

Code

 1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Web;
5 using System. Web. UI;
6 using System. Web. UI. WebControls;
7
8 using MSXML2;
9
10 public partial class Default4: System. Web. UI. Page
11 {
12 protected void Page_Load (object sender, EventArgs e)
13 {
14
15}
16 protected void button#click (object sender, EventArgs e)
17 {
18 // address of the Web service
19 string URL = "http: // localhost/YBWS/WebService. asmx ";
20
21 // splice data
22 string data;
23 data = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ";
24 data = data + "<soap12: Envelope xmlns: xsi = \" external "xmlns: xsd = \" http://www.w3.org/2001/XMLSchema\ "xmlns: soap12 = \" external "> ";
25 data = data + "<soap12: Body> ";
26 data = data + "<HelloWorld xmlns = \" http://tempuri.org/\ "/> ";
27 data = data + "</soap12: Body> ";
28 data = data + "</soap12: Envelope> ";
29
30 // create an asynchronous object (the XMLHTTP object is under MSXML2)
31 XMLHTTP xmlhttp = new XMLHTTP ();
32 xmlhttp. open ("POST", URL, false, null, null );
33 xmlhttp. setRequestHeader ("Content-Type", "application/soap + xml ");
34 xmlhttp. send (data );
35 Response. Write (System. Text. Encoding. UTF8.GetString (byte []) xmlhttp. responseBody ));
36 Response. End ();
37}
38}
39

When you click a button, the effect is as follows:

 

 

 

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.