The following code is for reference only. If you are interested, you can study it.
[1] simple data synchronization between mobile phones and servers
/// <Summary>
/// First pass the parameter (strImei, strCode) to the server, and then download LocalAllCategory. Xml to the mobile terminal ------- Method
/// </Summary>
/// <Param name = "URL"> server URL </param>
/// <Param name = "Filename"> local path </param>
/// <Param name = "strSortName"> category name </param>
/// <Param name = "strRightMark"> permission Code </param>
Public static void DownAllCategory (string URL, string Filename, string strImei, string strCode)
{
// Pass parameters to the server
String postData = strImei + "&" + strCode;
Byte [] bytes = Encoding. UTF7.GetBytes (postData); // note that UTF7 Encoding is required for Chinese Parameters
HttpWebRequest Myrq = (HttpWebRequest) WebRequest. Create (URL );
Myrq. Method = "POST ";
Myrq. ContentLength = bytes. Length;
Myrq. ContentType = "application/x-www-form-urlencoded ";
Try
{
Using (Stream requestStream = Myrq. GetRequestStream ())
{
RequestStream. Write (bytes, 0, bytes. Length );
RequestStream. Close ();
}
// Start File Download
HttpWebResponse myrp = (HttpWebResponse) Myrq. GetResponse ();
Long totalBytes = myrp. ContentLength;
// Prog. Maximum = (int) totalBytes;
Stream st = myrp. GetResponseStream ();
Stream so = new FileStream (Filename, FileMode. Create );
Long totalDownloadedByte = 0;
Byte [] by = new byte [1024];
Int osize = st. Read (by, 0, (int) by. Length );
While (osize> 0)
{
TotalDownloadedByte = osize + totalDownloadedByte;
Application. DoEvents ();
So. Write (by, 0, osize );
// Prog. Value = (int) totalDownloadedByte;
Osize = st. Read (by, 0, (int) by. Length );
}
So. Close ();
St. Close ();
// The download ends.
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message, "prompt", MessageBoxButtons. OK, MessageBoxIcon. Asterisk,
MessageBoxDefaultButton. Button1 );
}
}
[2] download an object [Http Communication]
/// <Summary>
/// Download method (with progress bar)
/// </Summary>
/// <Param name = "URL"> server URL </param>
/// <Param name = "Filename"> local path </param>
/// <Param name = "Prog"> progress bar </param>
Public string DownCategoryFile (string URL, string Filename, ProgressBar Prog)
{
Try
{
HttpWebRequest Myrq = (HttpWebRequest) HttpWebRequest. Create (URL );
HttpWebResponse myrp = (HttpWebResponse) Myrq. GetResponse ();
Long totalBytes = myrp. ContentLength;
Prog. Maximum = (int) totalBytes;
Stream st = myrp. GetResponseStream ();
Stream so