前一陣子在弄github的網站,發現在github上可以免費的建立一整套靜態網站。
而最近在弄一些個人的小軟件,然而軟件是需要不斷改進的,可是我又沒有個人服務器,那怎么辦?
這個時候我想到了github。
由於github是一個靜態網站,同時也是一個免費的雲盤,可以在上面存任何的東西,於是我就想能不能把最新的版本信息存在github上面,然后通過網頁的方式訪問指定的網頁從而獲取最新的版本號和下載地址?
經過一番小小的折騰,初步的模板已經完成:
基本流程是通過xml記錄版本號和下載地址-》上傳到github上-》再在客戶端通過網頁讀取xml信息-》把xml信息在本地解析成相應的類,從而獲得信息。
首先是一個開源的.net xml生成解析類,這是我在網上找到的一個開源的工具,自己稍微修改了一下,如果原作者不想再這里公開,可以聯系我。
這個xml類似通過.net的序列化來生成xml的,簡單方便快捷。所以就一直沿用了;
/* 2015.12.28 BobDong
* 生成xml文件,記錄生成的表名
*/
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace 模擬網站登錄
{
class XmlHelper
{
#region 序列化操作
/// <summary>
/// 序列化對象
/// </summary>
/// <param name="stream"></param>
/// <param name="o"></param>
/// <param name="encoding"></param>
private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding)
{
if (o == null)
throw new ArgumentNullException("o");
if (encoding == null)
throw new ArgumentNullException("encoding");
XmlSerializer serializer = new XmlSerializer(o.GetType());
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
NewLineChars = "\r\n",
Encoding = encoding,
IndentChars = " "
};
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, o);
writer.Close();
}
}
/// <summary>
/// 將一個對象序列化為XML字符串
/// </summary>
/// <param name="o">要序列化的對象</param>
/// <returns>序列化產生的XML字符串</returns>
public static string XmlSerialize(object o)
{
try
{
using (MemoryStream stream = new MemoryStream())
{
XmlSerializeInternal(stream, o, Encoding.UTF8);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
catch
{
return null;
}
}
/// <summary>
/// 以【Encoding.UTF8】反序列化xml
/// </summary>
/// <typeparam name="T">結果對象類型</typeparam>
/// <param name="s">包含對象的XML字符串</param>
/// <returns>反序列化得到的對象</returns>
public static T XmlDeserialize<T>(string s)
{
if (string.IsNullOrEmpty(s))
throw new ArgumentNullException("s");
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(s)))
{
using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
{
return (T)mySerializer.Deserialize(sr);
}
}
}
#endregion
}
}
接着是讀取網頁代碼的類,【log模塊被我刪掉了,需要的可以在相應的位置加上日志】
using System;
using System.IO;
using System.Net;
using System.Text;
namespace 模擬網站登錄
{
public static class WebData
{
public static T LoadData<T>(string path,Encoding encoding)
{
WebRequest request = WebRequest.Create(path);//實例化WebRequest對象
WebResponse response = request.GetResponse();//創建WebResponse對象
Stream datastream = response.GetResponseStream();//創建流對象
T resoult=default(T);
if (datastream == null)
{
return resoult;
}
StreamReader reader = new StreamReader(datastream, encoding);
string responseFromServer = reader.ReadToEnd();//讀取數據
reader.Close();
datastream.Close();
response.Close();
try
{
return XmlHelper.XmlDeserialize<T>(responseFromServer);
}
catch(Exception ex)
{
return resoult;
}
}
}
}
來一個最簡單的demo
這個是一個通過序列化得到的xml文件
<?xml version="1.0" encoding="utf-8"?> <string>1</string>
同時附上我的github測試地址:“http://www.bobdong.cn/Software/CodeTest/Test.txt”
private string Load()
{
return WebData.LoadData<string>("http://www.bobdong.cn/Software/CodeTest/Test.txt", Encoding.UTF8);
}
讀出來的數據就是一個字符串了;
【ps:demo只是一個簡單的例子,所以只放了一個string,其實也可以存放復雜類型的】
歡迎廣大園友雅批指正
welcome to my page:www.bobdong.cn
