This article reprinted: http://www.cnblogs.com/waynechan/archive/2012/06/25/2560685.html
1. What is WebService:
To put it simply, enterprises and websites access and use online services over the Internet. Some data cannot be provided to other organizations due to security issues, in this case, the WebService can be used to provide the service.
2. Create a WebService
After creating WebService, we can write the method for returning data in the file.
3. Four forms of returned data
The author's level is limited and only lists the return forms of the four types of data:
(1) directly return the DataSet object
(2) return the byte array after the DataSet object is serialized using binary.
(3) return the byte array after the datasetsurrogate object is serialized using binary.
(4) return the byte array after the datasetsurrogate object is serialized and zip in binary format.
Theoretically speaking, the network transmission byte and transmission time should be decreasing, among which (3) (4) method needs to reference the open source components provided by Microsoft: http://support.microsoft.com/kb/829740/zh-cn
The following shows the four types of returned dataCode(1) is the root of the three methods. You must obtain a dataset as the root, and then perform various conversion and compression operations:
[Webmethod (description = " Returns the DataSet object directly. " )]
Public Dataset getdataset ()
{
String Connstr = system. configuration. configurationmanager. connectionstrings [ " Conn " ]. Tostring ();
Sqlconnection conn = New Sqlconnection (connstr );
String SQL = " Select * From china_city " ;
Conn. open ();
Sqldataadapter SDA = New Sqldataadapter (SQL, Conn );
Dataset DS = New Dataset ( " China " );
SDA. Fill (DS );
Conn. Close ();
Return DS;
}
[Webmethod (description = " Directly return the DataSet object and use the byte array after binary serialization. " )]
Public Byte [] Getdatasetbytes ()
{
Dataset DS = getdataset ();
Binaryformatter SER = New Binaryformatter (); // Serialized object
Memorystream MS = New Memorystream (); // Memory stream
Ser. serialize (MS, DS );
Byte [] Buffer = Ms. toarray (); // Byte stream
Return Buffer;
}
[Webmethod (description = " Directly return the datasetsurrogate object and use the byte array after binary serialization. " )]
Public Byte [] Getdatasetsurrogatebytes ()
{
Dataset DS = getdataset ();
Datasetsurrogate DSS = New Datasetsurrogate (DS );
Binaryformatter SER = New Binaryformatter (); // Serialized object
Memorystream MS = New Memorystream (); // Memory stream
Ser. serialize (MS, DSS );
Byte [] Buffer = Ms. toarray (); // Byte stream
Return Buffer;
}
[Webmethod (description = " The datasetsurrogate object is directly returned, and the byte array serialized in binary and compressed in zip Mode " )]
Public Byte [] Getdatasetsurrogatezipbytes ()
{
Dataset DS = getdataset ();
Datasetsurrogate DSS = New Datasetsurrogate (DS );
Binaryformatter SER = New Binaryformatter (); // Serialized object
Memorystream MS = New Memorystream (); // Memory stream
Ser. serialize (MS, DSS );
Byte [] Buffer = Ms. toarray (); // Byte stream
Byte [] Bufferzip = compress (buffer );
Return Buffer;
}
// Compression Method
Public Byte [] Compress ( Byte [] Data)
{
Try
{
Memorystream MS = New Memorystream ();
Stream zipstream = Null ;
Zipstream = New Gzipstream (MS, compressionmode. Compress, True );
Zipstream. Write (data, 0 , Data. Length );
Zipstream. Close ();
Ms. Position = 0 ;
Byte [] Compressed_data = New Byte [Ms. Length];
Ms. Read (compressed_data, 0 , Int . Parse (Ms. length. tostring ()));
Return Compressed_data;
}
Catch
{
Return Null ;
}
}
We can view the effect of WebService in the browser. There are four methods available on this page. These four methods are the four methods we wrote to return data, click the method to return the corresponding data. In this way, we can write the code of the data provider. Next, we will write the method for calling the data!
Iv. Call data
Client WebServiceProgram
Private Void Button#click ( Object Sender, eventargs E)
{
Com. dzbsoft. www. service1 DS = New Com. dzbsoft. www. service1 (); // New WebService object
Datetime dtbegin = datetime. now;
Dataset dataset = Ds. getnorthwinddataset ();
This . Label1.text = String . Format ( " Time consumed: {0} " , Datetime. Now-dtbegin );
Binddata (Dataset );
}
Private Void Button2_click ( Object Sender, eventargs E)
{
Com. dzbsoft. www. service1 DS = New Com. dzbsoft. www. service1 ();
Datetime dtbegin = datetime. now;
Byte [] Buffer = Ds. getdatasetbytes ();
Binaryformatter SER = New Binaryformatter ();
Dataset dataset = Ser. deserialize ( New Memorystream (buffer )) As Dataset;
This . Label2.text = String . Format ( " Time consumed: {0} " , Datetime. Now-dtbegin) + " " + Buffer. length;
Binddata (Dataset );
}
Private Void Button3_click ( Object Sender, eventargs E)
{
Com. dzbsoft. www. service1 DS = New Com. dzbsoft. www. service1 ();
Datetime dtbegin = datetime. now;
Byte [] Buffer = Ds. getdatasetsurrogatebytes ();
Binaryformatter SER = New Binaryformatter ();
Datasetsurrogate DSS = Ser. deserialize ( New Memorystream (buffer )) As Datasetsurrogate;
Dataset dataset = DSS. converttodataset ();
This . Label3.text = String . Format ( " Time consumed: {0} " , Datetime. Now-dtbegin) + " " + Buffer. length;
Binddata (Dataset );
}
Private Void Button4_click ( Object Sender, eventargs E)
{
Com. dzbsoft. www. service1 DS = New Com. dzbsoft. www. service1 ();
Datetime dtbegin = datetime. now;
Byte [] Zipbuffer = Ds. getdatasetsurrogatezipbytes ();
Byte [] Buffer = unzipclass. Decompress (zipbuffer );
Binaryformatter SER = New Binaryformatter ();
Datasetsurrogate DSS = Ser. deserialize ( New Memorystream (buffer )) As Datasetsurrogate;
Dataset dataset = DSS. converttodataset ();
This . Label4.text = String . Format ( " Time consumed: {0} " , Datetime. Now-dtbegin) + " " + Zipbuffer. length;
Binddata (Dataset );
}
Private Void Binddata (Dataset dataset)
{
This . Datagridview1.datasource = dataset. Tables [ 0 ];
This . Label5.text = " Total: " + Dataset. Tables [ 0 ]. Rows. Count + " Records " ;
}
In the data return method, we use data compression. Therefore, on the caller side, we need to decompress the Code:
Client unzipclass Program
Public Static Class Unzipclass
{
Public Static Byte [] Decompress ( Byte [] Data)
{
Try
{
Memorystream MS = New Memorystream (data );
Stream zipstream = Null ;
Zipstream = New Gzipstream (MS, compressionmode. Decompress );
Byte [] Dc_data = Null ;
Dc_data = extractbytesfromstream (zipstream, Data. Length );
Return Dc_data;
}
Catch
{
Return Null ;
}
}
Public Static Byte [] Extractbytesfromstream (Stream zipstream, Int Datablock)
{
Byte [] DATA = Null ;
Int Totalbytesread = 0 ;
Try
{
While ( True )
{
Array. Resize ( Ref Data, totalbytesread + datablock + 1 );
Int Bytesread = zipstream. Read (data, totalbytesread, datablock );
If (Bytesread = 0 )
{
Break ;
}
Totalbytesread + = bytesread;
}
Array. Resize ( Ref Data, totalbytesread );
Return Data;
}
Catch
{
Return Null ;
}
}
}
In the above example, the effects of calling the four methods are the same. The only difference is that the data size and transmission time are different during transmission.