How to Use WinForm to call Web Service [Abstract]

Source: Internet
Author: User
NET WinForm calls Web Service

In. NET WinForm, Web Service calling is basically the same as Web Service calling in ASP. NET.

First, right-click the project and select "add Web reference" from the shortcut menu, as shown in 7-11.

After adding a reference, the project will also create a directory named Web References, that is, reference the proxy class, as shown in 7-12.

Figure 7-11 Add a Web reference figure 7-12 Web reference proxy class

Use this proxy class in the code.

ProductService. LTPService service = new ProductService. LTPService ();

String price = service. GetProductPrice ("001 ");

MessageBox. Show (price );

Microsoft's unified programming model makes development much easier.

Manually send HTTP request to call Web Service

However, what if I cannot directly add a reference to a Web Service that is not called in. NET? The following two examples do not directly call the Web Service through the proxy class.

Method 1: Get call

Private void button#click (object sender, EventArgs e)

{

String strURL = "http: // localhost: 12074/Service1.asmx/

GetProductPrice? ProductId = ";

StrURL + = this. textBox1.Text;

// Create an HTTP request

HttpWebRequest request = (HttpWebRequest) WebRequest. Create (strURL );

// Request. Method = "get ";

HttpWebResponse response = (System. Net. HttpWebResponse) request. GetResponse ();

Stream s = response. GetResponseStream ();

// Convert to XML for processing

XmlTextReader Reader = new XmlTextReader (s );

Reader. MoveToContent ();

String strValue = Reader. ReadInnerXml ();

StrValue = strValue. Replace ("& lt;", "<");

StrValue = strValue. Replace ("& gt;", "> ");

MessageBox. Show (strValue );

Reader. Close ();

}

Method 2: Post call

Private void button2_Click (object sender, EventArgs e)

{

String strURL = "http: // localhost: 12074/Service1.asmx/GetProductPrice ";

// Create an HTTP request

HttpWebRequest request = (HttpWebRequest) WebRequest. Create (strURL );

// Post Request Method

Request. Method = "POST ";

// Content type

Request. ContentType = "application/x-www-form-urlencoded ";

// Set parameters and perform URL Encoding

String paraurlcoded = httputility. urlencode ("productid ");

Paraurlcoded + = "=" + httputility. urlencode (this. textbox1.text );

Byte [] payload;

// Convert the URL-encoded string into bytes

Payload = system. Text. encoding. utf8.getbytes (paraurlcoded );

// Set the request contentlength

Request. contentlength = payload. length;

// Send a request to obtain the request stream

Stream writer = request. getrequeststream ();

// Write request parameters to the stream

Writer. Write (payload, 0, payload. Length );

// Close the request stream

Writer. Close ();

// Obtain the response stream

Httpwebresponse response = (httpwebresponse) request. getresponse ();

Stream S = response. getresponsestream ();

// Convert to XML for processing

Xmltextreader reader = new xmltextreader (s );

Reader. movetocontent ();

String strvalue = reader. readinnerxml ();

Strvalue = strvalue. Replace ("& lt;", "<");

Strvalue = strvalue. Replace ("& gt;", "> ");

MessageBox. Show (strvalue );

Reader. Close ();

}

The main difference between a GET request and a POST request is that the post parameter must be URL encoded and transmitted before the request is obtained, and get uses URL encoding to directly append the parameter to the request URL.

URL encoding is a character encoding format that ensures that the passed parameters are composed of consistent text (for example, encode a space as "% 20 ").

Since the Web Service returns a standard XML document, the basic principle is to process the returned XML data by yourself after a WebRequest request to obtain the desired information. At the same time, this method also has the advantage of dynamic calling of different WebService interfaces, which is more flexible.

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.