


Detailed introduction to XML Web Service graphic code examples
##For the first time I met XML Web Service and thought People who want to get started quickly may want to quickly understand how to create and call it. This article will use a small example to describe how to use Visual Studio 2008 to create Web Service and how to call it. The Web Service in the example will return an image according to the request of the client program.
1. CreateWeb ServiceProject
Open VS2008, select the File/New/Project menu item, in the opened New Project dialog box , select Visual C# -> Web -> ASP.NET Web Service Application, then enter the project name (Name), and the storage location (Position) and solution name (Solution Name), click "OK" to generate the project. In this example we use AnnotationWebService as the name of the project and solution (see Figure 1).
#Picture 1:New ProjectDialog box
2. Add aWeb Service
Click in Solution Explorer of VS2008 AnnotationWebService item, select the Project/Add new item menu item, in the opened Add New Item dialog box, select Web/Web Service, Then enter the name (Name) of Web Service and click "Add" to add a Web Service. In this example, we use ImageService as the name of Web Service (see Figure 2).
figure 2:Add New ItemDialog
#After that, we will see such a project directory in Solution Explorer (see Figure 3). (Note: When creating a project, the system will add a Web Service by default with the name Service1. You can click the Delete item in its right-click menu to delete it. delete.)
Picture 3:Solution Explorer
#3.
Encoding for Web Service Right-click ImageService.asmx and select View Markup to open this file. We can see the following line: It indicates that the code of ImageService is in ImageService. asmx.cs file. We right-click ImageService.asmx, select View Code, open the ImageService.asmx.cs file, and add our service code. In this example, we write a Method GetImage that reads the image based on the given file name and returns it to the client (see code below). ##<%@ WebService Language="C#" CodeBehind="ImageService.asmx.cs" Class="AnnotationWebService.ImageService" %>
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace AnnotationWebService
{
/// <summary>
/// Summary description for ImageService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class ImageService : System.Web.Services.WebService
{
[WebMethod(Description="Request an image by name")]
public byte[] GetImage(string imageFileName)
{
byte[] imageArray = GetBinaryFile(imageFileName);
if (imageArray.Length < 2)
{
throw new SoapException("Could not open image on server.", SoapException.ServerFaultCode);
}
else
{
return imageArray;
}
}
private byte[] GetBinaryFile(string fileName)
{
string fullPathFileName = HttpContext.Current.Request.PhysicalApplicationPath + fileName;
if (File.Exists(fullPathFileName))
{
try
{
FileStream fileStream = File.OpenRead(fullPathFileName);
return ConvertStreamToByteBuffer(fileStream);
}
catch
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}
public byte[] ConvertStreamToByteBuffer(Stream imageStream)
{
int imageByte;
MemoryStream tempStream = new MemoryStream();
while ((imageByte = imageStream.ReadByte()) != -1)
{
tempStream.WriteByte((byte)imageByte);
}
return tempStream.ToArray();
}
}
}
##4. InIISAdd virtual directory(Virtual Directory)
Open the IIS console program, right-click Default Web Site, and select Add New/Virtual Directory menu item , enter the virtual directory alias (Alias) in the opened Virtual Directory Caption Wizard dialog box, in this example we enter AnnotationWebService, click "Next ", then select the directory where ImageService.asmx is located, and then click "Next" until "Finish". (Note: The above description is based on the XP SP3 environment.)
##5. Create a proxy for Web Service(Proxy)
In VS2008, open a Windows application solution (.sln), in this example we Open a solution called AnnotationApp.Right-click on the project to call Web Service (for example, in this example we choose to use DataLib) and select the Add Web Reference menu item (if it has never been added After Web Reference, you may not see the Add Web Reference menu item. We can first select the Add Service Reference menu item, and then in the pop-up Add Click "Advanced" in the Service Reference dialog box, and then click "Add Web Reference" in the pop-up Service Reference Settings dialog box). In the Add Web Reference dialog box that pops up, enter the URL of the Web Service we want to call. In this example we enter:
##http://localhost/AnnotationWebService/ImageService.asmx
Then click "Go", ImageService will be displayed in the Web Page below, in Web reference nameEnter the name referenced by Web in the edit box. In order to avoid using the name ImageService again, here we enter ImageWebService (see Figure 4), and then click "Add Reference" to add a Web reference.
#Picture 4:Add Web ReferenceDialog box
This will add a Web Reference in Solution Explorer (see Figure 5).
#Picture 5: Web Reference was added
##The reference added is Image The proxy code of Service, which includes a class with the same name as ImageService, is derived from System.Web.Services.Protocols.SoapHttpClientProtocol. In this way, the GetImage method of ImageService can be called in the client code just like calling the method in its own Assembly.
6. 客户程序调用Web Service
在客户程序中需要调取图像的地方增加如下代码(注:代码中的Image类不是.Net Framework类库中的Image类,是客户程序中的一个类):
ImageService imageService = new ImageService(); Bitmap bitmap; try { byte[] image = imageService.GetImage("half-bred panthers.jpg"); MemoryStream memoryStream = new MemoryStream(image); bitmap = new Bitmap(memoryStream); _image = new Image(_viewportTransformer, bitmap); } catch (WebException e) { // Exception handling }
然后,可以将图像显示出来。
7.运行客户程序来测试Web Service调用
编译运行客户程序,Web Service被成功调用并返回所调用的图像(见图6)。
Figure 6: Running results
#
The above is the detailed content of Detailed introduction to XML Web Service graphic code examples. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and
