This task content
summary
Establish a web service to establish a client trial run reference for web services
SUMMARY This step guide describes how to use Microsoft ASP.NET and Microsoft Visual C # .NET to establish a Web service and a Web service client to send and receive binary documents. You can create a web service using ASP.NET and Visual C # .NET to save binary documents to folders on the web server and retrieve binary documents from the folder on the web server. You can use this service as a simple "document management system" on the Web.
Back to top
Establish web service
On the File menu in Microsoft Visual Studio .NET, click New, and then click Project. In the Visual C # project, select the ASP.NET Web service. Type or paste http: // localhost / docutp: // localhost / documentManagementService for your location, and then click OK. By default, service1.asmx is created and displayed in the design view. On the View menu, click the code to display the code view of Service1.asmx. Add the following WebMethods code to Service1 Class: [WebMethod]
Public bool savedocument (byte [] docbinaryArray, String Docname)
{
String strdocpath;
Strdocpath = "c: // DocumentDirectory //" DOCNAME
FileStream ObjfileStream = New FileStream (strDocpath, filemode.create, fileaccess.readwrite);
ObjfileStream.write (DocbinaryArray, 0, DocbinaryArray.length);
ObjFileStream.close ();
Return True;
}
[WebMethod]
Public int getDocumentlen (String Documentname)
{
String strdocpath;
StrDocpath = "c: // DocumentDirectory //" DocumentName
FileStream ObjfileStream = New FileStream (strDocpath, filemode.open, fileaccess.read);
INT LEN = (int) objfilestream.length;
ObjFileStream.close ();
Return Len;
}
[WebMethod]
Public Byte [] getDocument (String Documentname)
{
String strdocpath;
StrDocpath = "c: // DocumentDirectory //" DocumentName
FileStream ObjfileStream = New FileStream (strDocpath, filemode.open, fileaccess.read);
INT LEN = (int) objfilestream.length;
Byte [] DocumentContents = New Byte [LEN];
ObjfileStream.read (DocumentContents, 0, LEN);
ObjFileStream.close ();
Return DocumentContents;
}
Note: The above code saves documents to the
Back to top
Establish a client for the web service
On the File menu in Visual Studio .NET, click Add Item and click New Project. In the Visual C # item list, select Windows Applications, and then click OK. FORM1 is created by default. Add a web reference to the web service, as shown below:
In the Solution Explorer, right-click on the client project item. Then select Add Web references on the context menu. In the Add Web Reference dialog box, type the URL of the Web Services Description Language (WSDL) file pointing to the web service, then press ENTER. Note: The default location of the WSDL file is http://localhost/documentManagementService/service1.asmx? WSDL. In the Add Web Reference dialog box, click Add Reference. Add two buttons to FORM1. Set the text property of Button1 to "Store Documents on the server". Set the BUTTON2's text property to "Retrieve Document from Server." Double-click Button1 and Button2, create the default Click event handler for the button. Replace these processing programs with the following code: string sfile = "
Private void Button1_Click (Object Sender, System.Eventargs E)
{
FileStream Objfilestream = New FileStream (Sfile, FileMode.Open, FileAccess.Read);
INT LEN = (int) objfilestream.length;
Byte [] mybytearray = new byte [len];
ObjfileStream.read (MyByteArray, 0, LEN);
Localhost.Service1 myservice = new localhost.service1 ();
MyService.savedocument (MyByteArray, sfile.remove (0, sfile.lastIndexof ("//") 1);
ObjFileStream.close ();
}
Private void button2_click (Object Sender, System.Eventargs E)
{
MemoryStream objstreaminput = new memorystream ();
FileStream Objfilestream = New FileStream (Sfile.Insert (".LastIndexof (". ")," 2 "), FileMode.create, FileAccess.Readwrite;
Localhost.Service1 myservice = new localhost.service1 ();
INT LEN = (int) MyService.getDocumentlen (sfile.remove (0, sfile.lastIndexof ("//") 1)); byte [] mybyteaRray = new byte [len];
MyByteArray = myservice.getdocument (sfile.Remove (0, sfile.lastindexof ("//") 1);
ObjfileStream.write (MyByteaRray, 0, Len);
ObjFileStream.close ();
}
Note: The SFILE variable must contain the local file path to upload the document to the server. The document will be placed in the same folder after downloading and attached to the file name. Add the following namespace to the beginning of the file: Using System.IO; In the Solution Explorer, right-click the client project item. Then select "Set to Start Project" on the context menu.
Back to top
Trial operation
On the Debug menu, click Start. FORM1 will appear. Click the button labeled "Stored on the server". This will call the SaveDocument Web method. This web approach stores local documents in the
The information in this article applies to:
Microsoft ASP.NET (Included with the .NET Framework 1.0 Microsoft Visual C # .NET (2002)