2018年2月8日 星期四

【C#】API url parameter 參數 Encode 有趣的規範 cp950

【C#】API  url  parameter 參數 Encode 有趣的規範

web Client parameter 有含 中文 ,請注意 Api Server 系統 是utf8 接還是 big5 接 你的 參數

如果 api server 是  utf8 ,那很好,因為 html url 本身就是 follower utf8 格式

若 API SERVER 是BIG5 接 參數,請注意,
WEB CLIENT 的程式, 參數必需要再轉成 CP950 encode 編碼

如下:
Encoding cp950 = System.Text.Encoding.Default;
cp950 = Encoding.GetEncoding(950);

 string a = HttpUtility.UrlEncode(item.Value, cp950);  <----url big5="" nbsp="" p="">

為何要兩道手序,
因為  C# Encoding 有(utf8、utf16、utf32、ASCII) ,
就是沒有 BIG5格式

透過 Encoding GetEncoding 參數 950 幫我們抓 big5 格式回來..
=.=" 果然是美國人寫的程式...不是說要國際化嗎?

另外 網頁的 encodeURIComponent 字串會轉換成 UTF-8

所以程式接的時候是  HttpUtility UrlDecode(url)

但要再轉發 API 的話 就要看 對方  SERVER (參數)吃什麼  ContenType (utf8 or big5 ...)

以上

【C#】不能 var --->System.Web.HttpUtility.ParseQueryString (NameValueCollection)

【C#】不能 var  --->System.Web.HttpUtility.ParseQueryString (NameValueCollection)

要明確然指定 NameValueCollection