How to consume LEADTOOLS WCF as REST services using WebRequest and WebResponse (C#, VB.Net)
Method 1:
Start Visual Studio .NET.
Choose File->New->Project... from the menu.
In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
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.
From the Visual Studio Toolbox, create a standard button and name it to 'Method 1'.
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:
Imports System.NetImports System.Text
using System.Net;using System.Text;
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 HttpWebResponseTryresponse = DirectCast(request.GetResponse(), HttpWebResponse)Catch web As WebExceptionresponse = DirectCast(web.Response, HttpWebResponse)End Try
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;}
Method 2:
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'.
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:
Imports System.IO
using System.IO;
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 = 0Dim ms As MemoryStream = New MemoryStream()Dim response As HttpWebResponseTry response = CType(request.GetResponse(), HttpWebResponse)Dim responseStream As Stream = response.GetResponseStream()Dim bytesRead As IntegerbytesRead = 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 WhileFile.WriteAllBytes("c:\\result.jpeg", ms.ToArray())Catch web As WebExceptionresponse = CType(web.Response, HttpWebResponse)End Try
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;}
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
