Update Windows Mobile Application via your Http Server
Um, I haven't touch this Blog for a while... So... just a case study. Usually, I use FTP to find available files to update my mobile application. However,I have a client who doesn't want to install FTP but they have web server. Therefore, I use the traditional way to update the application, "Make a http query"... It is pretty simple to use HttpWebRequest and HttpWebResponse to validate the version. If the server returns a file link, then I download it and install it.
You can make a HttpWebRequest (http://yourwebsite/?version=1.0.2.0) to find available update. If server redirects you to a cab file, so we have an update.
Once user clicks "Yes", the we start to download and install the cab file.
Have a look the following example in C#. It handles downloading the cab file with progress bar.
privatevoidbackgroundWorker1_DoWork(objectsender,BCD.ComponentModel.DoWorkEventArgse){HttpWebRequestmyRequest=null;HttpWebResponsemyResponse=null;stringnewurl=(string)e.Argument;stringfileName="",filePath="";try{boolredirecting=true;while(redirecting){try{myRequest=(HttpWebRequest)WebRequest.Create(newurl);// we accept any type of filemyRequest.Accept="*/*";// set the timeout to 5 secondsmyRequest.Timeout=5000;// casts the responsemyResponse=(HttpWebResponse)myRequest.GetResponse();// if we have redirectionif((int)myResponse.StatusCode==301||(int)myResponse.StatusCode==302){stringuriString=myResponse.Headers["Location"];newurl=uriString;// and keep going}else{// gets the final urifileName=Path.GetFileName(myResponse.ResponseUri.LocalPath);// we only want CAB fileif(fileName.ToLower().EndsWith(".cab")){filePath=Path.Combine(Utils.TempPath,fileName);// gets the total lenth for progress barlongfileLength=myResponse.ContentLength;// start with zerointbyteTotal=0;// start writing fileusing(FileStreamfileStream=File.OpenWrite(filePath)){// gets the stream from response objectusing(StreamremoteStream=myResponse.GetResponseStream()){// we make 4 MB as our bufferbyte[]inBuffer=newbyte[4*1024];intnRead=remoteStream.Read(inBuffer,0,inBuffer.Length);// we need to put e.Cancel as user may cancel downloadingwhile(nRead>0&&!e.Cancel){if(nRead>0)fileStream.Write(inBuffer,0,nRead);byteTotal+=nRead;// calculate the progress out of a base "100"doublepercentage=(double)(byteTotal)/(double)fileLength;// update the progressbackgroundWorker1.ReportProgress((int)(percentage*100));nRead=remoteStream.Read(inBuffer,0,inBuffer.Length);}myResponse.Close();myRequest.Abort();}}// if everything is fineif(!e.Cancel){// write the file time to be same as the original file timeBCD.IO.FileEx.SetCreationTime(filePath,myResponse.LastModified);e.Result=filePath;}else{if(File.Exists(filePath))File.Delete(filePath);}}redirecting=false;}}catch(Exceptionex){redirecting=false;Utils.WriteToLog(ex);}}}catch(Exceptionex){// if error happens on retriving map, assign the error image insteadUtils.WriteToLog(ex);}finally{if(myResponse!=null)myResponse.Close();myRequest=null;myResponse=null;}}
The standard paragraphs Welcome to this demo page! Here, you’ll get an exclusive preview of our cutting-edge platform designed to revolutionise your digital experience. Our...