Implement file transfer with user authentication Web Service

xiaoxiao2021-03-06  37

First, understand Web Service

Let us first understand the Web Service.

Web Services is actually based on XML-based system services for business, applications. In fact, it is a new concept based on a series of existing technology. Using it as used using RPC (remote procedure call), but the interface it provides is object-based. It is with the original component model, such as COM / DCOM, CORBA, RMI, etc., the biggest feature is the generality of the cross-platform that standardizes (based on XML-based series), the unobstructed ability to http ( Cross the firewall), for DOT NET, the Web Service in communication with Visual Studio is also easy to use. User even does not understand those related criteria, as long as it will become objects, there is a preliminary understanding of Web Service to write Web Service.

How to build a web service? The following technical steps have been widely recognized in the industry.

1. Service providers build, organize and release network services. Its means is diverse, and can be done using programming languages ​​(such as Delphi, Java, C #, etc.), middleware, or other special platforms.

2. Service providers provide service description with WSDL (The Web Services Description Language). The WSDL document provides a service description to third parties.

3. Service providers Register online services to UDDI (Universal Description, Discovery, and Integration) registry. UDDI enables developers to publish services and can search for other people from other people. Users will search for services by searching for the UDDI registry.

4. The client application is bound to the network service and activate the corresponding operations of the network service via SOAP (The Simple Object Access Protocol). SOAP provides parameters and return results in XML format and transmits with HTTP. All network services communicate through SOAP.

The first step is the core, which determines your web service to use and interface. The second step is complete in Visual Studio.NET, and you can of course manually generate with WSDL.exe. The fourth step provides the greatest convenience when programming when programming. Users only use "Add Web Service Reference" or "Add Web Service Reference" (Chinese Version), typing the URL, Visual Studio.net automatically generates class that calls the Web Service, and the underlying SOAP is completely transparent to the user.

However, when the user needs to consider safety, it has to consider SOAP. Remember, security is to consider in the future. In the future, software development becomes configured integration, software products become service, purchase software becomes rental software, most of the services are based on the network, you may have to consider who can use you Service, who can't use your service, of course, I also agree with open source and public exchange technology, I wrote my own hard-crispy things and show my attitude. However, after all, we still have to rely on the software to eat :)

In Web Service, the user name, password can be transmitted through the SOAP header. In Web Service, you need to process the SOAP header, but this is not too difficult. Everyone realizes this and then looks at the example below, you can learn how to use it.

Second, the code implementation First, we implements a class for authentication, file name authentication.cs as follows:

using System; namespace useResData {/// /// achieve file transfer with user authentication Web Service /// public class Authentication: System.Web.Services.Protocols.SoapHeader {public string Username; public string Password; public Boolean Validuser (string in_username, string in_password) {if ((in_username == "caomo) && (in_password ==" password "))) {Return true;} else {return false;}} public authentication ()}} TODO: Add Constructor Logic Here //}}} Let's generate a web service, named FileServer, there is the following code in FileServer.asmx:

<% @ WebService language = "c #" codebehind = "fileserver.asmx.cs" class = "useresdata.fileserver"%>

Everyone can see how the codebehind technology is used. In Visual Studio.net, the automatically generated code uses a large number of statements. It makes the design page and the write code are all open.

In FILeServer.asmx.cs, the code is as follows:

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using System.IO; namespace useResData {/// /// Summary . description for FileServer /// public class FileServer: System.Web.Services.WebService {private string rootdir; public FileServer () {// CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent (); rootdir = Server.mappath ("/ caomo / providing file");} #Region Component Designer generated code ///// Required method for designer support - do not modify /// The contents of this method with the code editor. /// private void initializeComponent () {} #ENDREGON / / / / / CLEAN UP Any Resources Being Used. // Protected Override Void Dispose (Bool Disposing) {} PUBLIC Authentication Header; / / Define User Authentication Class Variables HEADER. [WebMethod (Description = "NEED Authentication!")] [System.Web.Services.protocols.soapheader ("Header")] // User authentication SOAP head Public String getfile (String filepath) {if (Header.Validuser header.Username, header.Password)) // user authentication {FileStream myfile = File.OpenRead (rootdir filePath); BinaryReader br = new BinaryReader (myfile); byte [] btBuf = new byte [myfile.Length]; long i = 0; while (br.peekchar ()> - 1) {btbuf [i] = br.readbyte (); i ;} myfile.close (); return system.convert.tobase64string (btbuf);} else returnf);} else returnf ; // User Authentication Failed} Run it. Will get the page shown in Figure 1:

figure 1

Everyone should pay attention to the service named GetFile is the Web Method in my code. The following "NEED Authentication!" Is given by Description = "NEED Authentication!" In the WebMethod definition.

Here you click "getfile" We go to the next page, everyone sees the page shown in Figure 2 is not the same as the general web service call page?

Figure 2 In order to let everyone see the difference with user verification and no user verification, we can release the code line of the annotated note with the user authentication, and the same page that everyone enters will be shown in Figure 3: Figure 3 You can see that when you don't have verification, you can use the HTTP GET to be called directly through the IE automatically generated, and the result is an XML file (SOAP response file). As shown in Figure 4:

Figure 4 The file is returned by the encoding, look at the horizontal scroll bar, the code of this file is all in one line. OK, let's so on, as for the code of the file transfer section, I don't explain, it is worth noting that this file transfer function supports the file transfer of the text form is very good, and the binary file is greater than 4K. There will be a buffer overflow, and if anyone has a better way, you may wish to contact the author caomo@chinaren.com. Finally, how to prepare the client, huh, because the author is limited, and listening to the decomposition. You can try yourself first.

转载请注明原文地址:https://www.9cbs.com/read-67874.html

New Post(0)