GETTING FROM REST SERVICES in Visual Basic .NET

Generator QR Code in Visual Basic .NET GETTING FROM REST SERVICES

GETTING FROM REST SERVICES
Creating Quick Response Code In VB.NET
Using Barcode creation for .NET Control to generate, create QR Code image in Visual Studio .NET applications.
www.OnBarcode.com
Scanning QR In Visual Basic .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
In relation to Silverlight, although REST may dictate the method in which a resource is accessed, it doesn t dictate the format of the data received. The most common ways to return data from a RESTful web service are Plain Old XML (POX) and JSON. We ll discuss how to consume both POX and JSON in section 14.3. The basics of calling a REST-based web service from Silverlight involve creating an HttpWebRequest object, setting its destination URI, and calling it asynchronously (see listing 14.6).
Print Linear 1D Barcode In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create Linear 1D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
GTIN - 128 Printer In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create USS-128 image in .NET framework applications.
www.OnBarcode.com
Listing 14.6 Getting data from a REST service
EAN-13 Generator In VB.NET
Using Barcode creation for .NET Control to generate, create UPC - 13 image in .NET framework applications.
www.OnBarcode.com
Barcode Creation In VB.NET
Using Barcode drawer for .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
private void GetSingleXml_Click(object sender, RoutedEventArgs e) { UIThread = SynchronizationContext.Current; string rawPath = "http://www.silverlightinaction.com/Authors.svc/SingleXml/{0}"; Uri path = new Uri(string.Format(rawPath, Input.Text), UriKind.Absolute); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path); request.BeginGetResponse(SingleXmlCallBack, request); } private void SingleXmlCallBack(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); Stream responseStream = response.GetResponseStream(); UIThread.Post(UpdateUiText, responseStream); }
Code 39 Full ASCII Encoder In VB.NET
Using Barcode drawer for .NET framework Control to generate, create USS Code 39 image in .NET framework applications.
www.OnBarcode.com
USPS PLANET Barcode Generation In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Planet image in .NET applications.
www.OnBarcode.com
In this example, you make a simple request to a RESTful web service. Three steps are necessary when making a GET request, all of which are demonstrated here:
QR Code 2d Barcode Reader In Visual C#
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
QR Code JIS X 0510 Encoder In None
Using Barcode maker for Font Control to generate, create QR Code JIS X 0510 image in Font applications.
www.OnBarcode.com
1 2 3
Printing 1D Barcode In Java
Using Barcode creation for Java Control to generate, create Linear 1D Barcode image in Java applications.
www.OnBarcode.com
Data Matrix 2d Barcode Creator In Java
Using Barcode generation for Android Control to generate, create Data Matrix 2d barcode image in Android applications.
www.OnBarcode.com
Create a Uri object and initialize it with the path and, optionally, the UriKind. Create an HttpWebRequest object for the Uri. Call BeginGetResponse on your HttpWebRequest object and pass it the name of a callback method, as well as the HttpWebRequest itself.
Barcode Creator In None
Using Barcode creation for Microsoft Word Control to generate, create Barcode image in Office Word applications.
www.OnBarcode.com
Drawing GS1 DataBar Limited In .NET Framework
Using Barcode creator for Visual Studio .NET Control to generate, create GS1 DataBar-14 image in .NET framework applications.
www.OnBarcode.com
Download from Wow! eBook <www.wowebook.com>
Painting DataMatrix In Visual C#.NET
Using Barcode generation for .NET framework Control to generate, create ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
Create 1D Barcode In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create 1D Barcode image in ASP.NET applications.
www.OnBarcode.com
Networking and communications
2D Generator In VS .NET
Using Barcode generation for .NET Control to generate, create 2D image in Visual Studio .NET applications.
www.OnBarcode.com
Encode Barcode In None
Using Barcode maker for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
The BeginGetResponse method initiates the call to the service and registers the passed-in method as a callback method. When the response returns, that method will be called with the current HttpWebRequest being passed to it as type IAsyncResult. In the callback method, the first thing is to cast the AsyncState of the IAsyncResult into an HttpWebRequest object. In the next statement, you call the EndGetResponse method on the request object to both end the connection and return an HttpWebResponse object. Finally, you call the GetResponseStream method of the HttpWebResponse object to get the Stream, the response to your web service call. We ll cover deserializing the Stream into useful data in section 14.3.
Paint EAN / UCC - 13 In None
Using Barcode generation for Font Control to generate, create GTIN - 13 image in Font applications.
www.OnBarcode.com
Drawing Barcode In Objective-C
Using Barcode generation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
POSTING TO REST SERVICES
Most RESTful services use GET to retrieve data and POST to send it. Because the default HTTP verb used when using HttpWebRequest is GET, you need to do a few things differently when you want to perform a POST. Listing 14.7 shows the process of sending data to a REST service.
Listing 14.7 POSTing data to a REST service
private void Test_Click(object sender, RoutedEventArgs e) { UIThread = SynchronizationContext.Current; Uri path = new Uri("http://www.silverlightinaction.com/Authors.svc/Update/Brown", UriKind.Absolute); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path); request.Method = "POST"; request.ContentType = "application/xml"; request.BeginGetRequestStream(AddPayload, request); } private void AddPayload(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest) result.AsyncState; StreamWriter dataWriter = new StreamWriter(request.EndGetRequestStream(result)); dataWriter.Write("< xml version=\"1.0\" ><Author><FirstName>Bob" + "</FirstName><LastName>Smith</LastName></Author>"); dataWriter.Close(); request.BeginGetResponse(SingleJsonCallBack, request); } private void SingleJsonCallBack(IAsyncResult result) { HttpWebRequest request = (HttpWebRequest)result.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); Stream responseStream = response.GetResponseStream(); UIThread.Post(UpdateUiText, responseStream); }
Because REST services don t have methods, and instead deal with entities, you need to add any data to be sent to the service to the message being sent. In listing 14.7, instead
Download from Wow! eBook <www.wowebook.com>
The client HTTP stack
of calling BeginGetResponse from the initial call, you call BeginGetRequestStream. This event handler allows you to add information to the stream after it s created but before it s sent to the service. After that s been done, you register the BeginGetResponse event handler as is done during GET operations. Knowing how to do GETs and POSTs is only half of the battle; you need to be able to use what gets returned as well. REST services normally return either XML- or JSONformatted data. In section 14.4, we ll talk about ways to take the response stream containing these common data formats and convert it into useful objects. The browser stack only allows POST and GET, not DELETE or PUT. Those limitations, and the need for out-of-browser networking support, prompted the team to create a second separate networking stack: the client HTTP Stack.
Copyright © OnBarcode.com . All rights reserved.