How to consume LEADTOOLS WCF as REST services using WebRequest and WebResponse (C#, VB.Net)
- Method 1:
-
1. Start Visual Studio .NET.
2. Choose File->New->Project... from the menu.
3. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
4. Type the project name as "ConvertImage_Service" in the Project Name field, and then choose OK. If desired, type a new location for
your project or select a directory using the Browse button, and then choose OK.
5. From the Visual Studio Toolbox, create a standard button and name it to 'Method 1'.
6. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning
of the file:
[Visual Basic]
Imports System.Net Imports System.Text
[C#]
using System.Net; using System.Text;
7. Add the following code to the Button's Click function:
[Visual Basic]
Dim baseAddress As String = "http://localhost:8080/LeadtoolsServicesHost/RasterService.svc" Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(baseAddress & "/Convert"),_ HttpWebRequest) request.Method = "POST" request.ContentType = "text/xml; charset=utf-8" Dim reqBodyStr As String = "<ConvertRequestxmlns=""http://Leadtools.Services.Raster.ServiceContracts/2009/01""" reqBodyStr = reqBodyStr & "><ConvertOptions xmlns:a=""http://Leadtools.Services.Raster.DataContracts/2009/01""" reqBodyStr = reqBodyStr & " xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><a:BackColor i:nil=""true""/>" reqBodyStr = reqBodyStr & "<a:BitsPerPixel>16</a:BitsPerPixel><a:Destination i:type=""b:FileBinaryData"" xmlns:b=""" reqBodyStr = reqBodyStr & "http://Leadtools.Services.DataContracts/2009/01""><b:FileName>c:\out.cmp</b:FileName></a:Destination>" reqBodyStr = reqBodyStr & "<a:FirstPage>1</a:FirstPage><a:Format>CMP</a:Format><a:Height>0</a:Height><a:HorizontalAlignMode>Near" reqBodyStr = reqBodyStr & "</a:HorizontalAlignMode><a:LastPage>-1</a:LastPage><a:QualityFactor>2</a:QualityFactor><a:ResizeFlags>" reqBodyStr = reqBodyStr & "None</a:ResizeFlags><a:ResizeMode>Normal</a:ResizeMode><a:Source i:type=""b:FileBinaryData"" xmlns:b=""" reqBodyStr = reqBodyStr & "http://Leadtools.Services.DataContracts/2009/01""><b:FileName> image1.jpeg </b:FileName></a:Source>" reqBodyStr = reqBodyStr & "<a:VerticalAlignMode>Near</a:VerticalAlignMode><a:Width>0</a:Width></ConvertOptions></ConvertRequest>" Dim reqBodyBytes As Byte() = Encoding.UTF8.GetBytes(reqBodyStr) request.GetRequestStream().Write(reqBodyBytes, 0, reqBodyBytes.Length) request.GetRequestStream().Close() Dim response As HttpWebResponse Try response = DirectCast(request.GetResponse(), HttpWebResponse) Catch web As WebException response = DirectCast(web.Response, HttpWebResponse) End Try
[C#]
string baseAddress = "http://localhost:8080/LeadtoolsServicesHost/RasterService.svc"; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress + "/Convert"); request.Method = "POST"; request.ContentType = "text/xml; charset=utf-8"; string reqBodyStr = "<ConvertRequest xmlns=\"http://Leadtools.Services.Raster.ServiceContracts/2009/01\">"; reqBodyStr += "<ConvertOptions xmlns:a=\"http://Leadtools.Services.Raster.DataContracts/2009/01\" "; reqBodyStr += "xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><a:BackColor i:nil=\"true\"/>"; reqBodyStr += "<a:BitsPerPixel>16</a:BitsPerPixel><a:Destination i:type=\"b:FileBinaryData\" xmlns:b=\""; reqBodyStr += "http://Leadtools.Services.DataContracts/2009/01\"><b:FileName>c:\\out.cmp</b:FileName></a:Destination>"; reqBodyStr += "<a:FirstPage>1</a:FirstPage><a:Format>cmp</a:Format><a:Height>0</a:Height><a:HorizontalAlignMode>Near"; reqBodyStr += "</a:HorizontalAlignMode><a:LastPage>-1</a:LastPage><a:QualityFactor>2</a:QualityFactor><a:ResizeFlags>"; reqBodyStr += "None</a:ResizeFlags><a:ResizeMode>Normal</a:ResizeMode><a:Source i:type=\"b:FileBinaryData\" xmlns:b=\""; reqBodyStr += "http://Leadtools.Services.DataContracts/2009/01\"><b:FileName>c:\\image1.jpeg</b:FileName></a:Source>"; reqBodyStr += "<a:VerticalAlignMode>Near</a:VerticalAlignMode><a:Width>0</a:Width></ConvertOptions></ConvertRequest>"; byte[] reqBodyBytes = Encoding.UTF8.GetBytes(reqBodyStr); request.GetRequestStream().Write(reqBodyBytes, 0, reqBodyBytes.Length); request.GetRequestStream().Close(); HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException web) {
response = (HttpWebResponse)web.Response; }
8. Build, and Run the program to test it.
- Method 2:
-
1. Continue to use the project created in Method 1 above. From the Visual Studio Toolbox, create a standard button and name it to 'Method 2'.
2. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning of the file:
[Visual Basic]
Imports System.IO
[C#]
using System.IO;
3. Add the following code to the 'Method 2' Button's Click function:
[Visual Basic]
Dim request As HttpWebRequest = CType(HttpWebRequest.Create("http://localhost:8080/LeadtoolsServicesHost/RasterService.svc/ConvertImage? uri=\\MyMachine\SharedFolder\1.tif&format=jpeg"), HttpWebRequest) request.Method = "GET" Dim buffer() As Byte = New Byte((1024 * 64) - 1) {} Dim totalBytesRead As Integer = 0 Dim ms As MemoryStream = New MemoryStream() Dim response As HttpWebResponse Try response = CType(request.GetResponse(), HttpWebResponse) Dim responseStream As Stream = response.GetResponseStream() Dim bytesRead As Integer bytesRead = responseStream.Read(buffer, 0, buffer.Length) While (bytesRead > 0) bytesRead = responseStream.Read(buffer, 0, buffer.Length) totalBytesRead += bytesRead ms.Write(buffer, 0, bytesRead) bytesRead = responseStream.Read(buffer, 0, buffer.Length) End While File.WriteAllBytes("c:\\result.jpeg", ms.ToArray()) Catch web As WebException response = CType(web.Response, HttpWebResponse) End Try
[C#]
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:8080/LeadtoolsServicesHost/RasterService.svc/ConvertImage? uri=\\\\MyMachine\\SharedFolder\\1.tif&format=jpeg"); req.Method = "GET"; byte[] buffer = new byte[1024 * 64]; int totalBytesRead = 0; MemoryStream ms = new MemoryStream(); HttpWebResponse resp; try { resp = (HttpWebResponse)req.GetResponse(); Stream responseStream = resp.GetResponseStream(); int bytesRead; while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0) { totalBytesRead += bytesRead; ms.Write(buffer, 0, bytesRead); } File.WriteAllBytes("c:\\result.jpeg", ms.ToArray()); } catch (WebException web) { resp = (HttpWebResponse)web.Response; }
4. Build, and Run the program to test it.