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.Net
Imports System.Text
using System.Net;
using System.Text;
Add the following code to the Button's Click function:
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
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;
}
Build, and Run the program to test it.
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;
Add the following code to the 'Method 2' Button's Click function:
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
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;
}
Products |
Support |
Feedback: How to Consume LEADTOOLS WCF as REST Services |
Introduction |
Help Version 19.0.2017.6.21
|
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
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.