Consume WCF REST Services using GET Operations (with Windows Forms Client)

by Jagadish Pulakhandam on 1/26/2012 1:50:05 PM
Rated 0 from 0 votes
Brief: Demonstrates consuming a WCF REST service using a simple Windows Forms Client Application
Posted to: Consuming WCF REST Services
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

The sample demonstrates the following:
  • How to fetch data from WCF REST service using Windows Forms (using HTTP GET operations)
  • How to use WebRequest class to fetch from WCF REST service
  • How to use HttpWebRequest and HttpWebResponse to work with WCF REST service
  • Working 'Stream' object returned by HttpWebResponse
NOTE: The samples use POX (Plain Old XML) to communicate to WCF REST service.  The source code of WCF REST Service is already provided in previous articles.

Screen shot:



Source Code (in VB):

01.Imports System.Net
02.Imports System.IO
03.Imports System.Text
04. 
05.Public Class Form1
06. 
07.    Private Sub btnGetEmployeeDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetEmployeeDetails.Click
08.        Dim uri As String = "http://localhost:8080/SampleEmpService/Employees/" & txtEmpno.Text
09.        Dim req As HttpWebRequest = WebRequest.Create(uri)
10.        Dim resp As HttpWebResponse = req.GetResponse()
11.        Dim respStream As Stream = resp.GetResponseStream()
12. 
13.        Dim readStream As New StreamReader(respStream, Encoding.UTF8)
14.        MessageBox.Show(readStream.ReadToEnd())
15. 
16.        'Dim xdoc As XElement = XElement.Load(respStream)
17.        'Me.lblEname.Text = xdoc.<Ename>.Value
18.        'Me.lblSal.Text = xdoc.<Sal>.Value
19.        'Me.lblDeptno.Text = xdoc.<Deptno>.Value
20. 
21.        respStream.Close()
22.        resp.Close()
23. 
24.    End Sub
25. 
26.End Class

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following