C # implement communication between the mobile phone end and the server end

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.