GetRemoteXmlFile.ashx
出自ProgWiki
用途
- 可取得遠端網站的Xml檔案, 並將Xml的語系進行轉碼(可繁簡体互轉)
程式碼
- 檔案: GetRemoteXmlFile.ashx
<%@ WebHandler Language="C#" Class="GetRemoteXmlFile" %> using System; using System.Web; using System.Net; using System.IO; using System.Text; using System.Runtime.InteropServices; //簡体轉繁體範例 //http://localhost/GetRemoteXmlFile.ashx?src=GBK&dest=BIG5&url=http://localhost/gbk.xml //繁體轉簡体範例 //http://localhost/GetRemoteXmlFile.ashx?src=BIG5&dest=GBK&url=http://localhost/big5.xml public class GetRemoteXmlFile : IHttpHandler { public void ProcessRequest (HttpContext context) { string strLangSrc = ""; string strLangDest = ""; string strSrcUrl = ""; string strText = ""; Encoding encode_src; Encoding encode_dest; try { //取得來源的URL, 與指定來源與目的的語系 strSrcUrl = context.Request["url"]; strLangSrc = context.Request["src"]; strLangDest = context.Request["dest"]; try { encode_src = Encoding.GetEncoding(strLangSrc); } catch { encode_src = Encoding.Default; } strText = GetWebFile(strSrcUrl, encode_src); } catch { } try { encode_dest = Encoding.GetEncoding(strLangDest); } catch { encode_dest = Encoding.Default; } if (strLangSrc != strLangDest) { try { encode_src = Encoding.GetEncoding(strLangSrc); } catch { encode_src = Encoding.Default; } if ((strLangSrc.ToLower() == "gbk") || (strLangSrc.ToLower() == "gb2312")) { strText = ConvertStringS2T(strText); } Byte[] arTemp = Encoding.Convert(encode_src, encode_dest, encode_src.GetBytes(strText)); strText = encode_dest.GetString(arTemp); } if ((strLangDest.ToLower() == "gbk") || (strLangDest.ToLower() == "gb2312")) { strText = ConvertStringT2S(strText); } context.Response.ContentType = "text/xml"; context.Response.ContentEncoding = encode_dest; string str1 = string.Format(" encoding=\"{0}\"", strLangSrc); string str2 = string.Format(" encoding=\"{0}\"", strLangDest); strText = strText.Replace(str1, str2); // context.Response.Write(strText); } //以指定語系的方式, 取得遠端網站的xml檔案 public string GetWebFile(string strUrl, Encoding encode_src) { string strRet = ""; Uri uri = new Uri(strUrl); HttpWebRequest hwReq = WebRequest.Create(uri) as HttpWebRequest; using (HttpWebResponse hwRes = hwReq.GetResponse() as HttpWebResponse) { hwReq.Method = "GET"; hwReq.KeepAlive = false; hwReq.Timeout = 10000; StreamReader reader = new StreamReader(hwRes.GetResponseStream(), encode_src); strRet = reader.ReadToEnd(); } return strRet; } public bool IsReusable { get { return false; } } [DllImport("kernel32.dll", EntryPoint = "LCMapStringA")] public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest); const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000; const int LCMAP_TRADITIONAL_CHINESE = 0x04000000; //繁體轉簡体 public static String ConvertStringT2S(String lines) { Encoding gb2312 = Encoding.GetEncoding(936); byte[] src = gb2312.GetBytes(lines); byte[] dest = new byte[src.Length]; LCMapString(0x0804, LCMAP_SIMPLIFIED_CHINESE, src, -1, dest, src.Length); return gb2312.GetString(dest); } //簡体轉繁體 public static String ConvertStringS2T(String lines) { Encoding gb2312 = Encoding.GetEncoding(936); byte[] src = gb2312.GetBytes(lines); byte[] dest = new byte[src.Length]; LCMapString(0x0804, LCMAP_TRADITIONAL_CHINESE, src, -1, dest, src.Length); return gb2312.GetString(dest); } }