後來參考保哥文章"關於個瀏覽器對網頁與文字編碼的處理規則研究整理"一文,以及另外一篇網路文章"C#, 使用 HttpWebRequest 向網站提交資料",終於把惱人的亂碼問題給解決掉。其實會一直失敗的原因主要是因為透過WebRequest.Create(requestUriString)時,requestUriString必須將字串進行UrlEncode為Big5碼的動作才行。
以下是自己的Post Big5碼資料的Method供參考
01 | /// <SUMMARY> |
02 | /// This method is used to post data to remote host via WebRequest |
03 | /// </SUMMARY> |
04 | /// |
05 | private void postFormViaWebRequest(Dictionary<STRING,STRING> inpufFileds, string sendURL) |
06 | { |
07 | Encoding encoding = Encoding.GetEncoding(950); |
08 | string postData = string.Empty; |
09 | foreach (KeyValuePair<STRING,STRING> kvp in inpufFileds) |
10 | { |
11 | postData += "&" + HttpUtility.UrlEncode(kvp.Key, encoding) + "=" + HttpUtility.UrlEncode(kvp.Value, encoding); |
12 | } |
13 | byte[] data = Encoding.ASCII.GetBytes(postData); |
14 | HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(sendURL + "?"); |
15 | postRequest.Method = "POST"; |
16 | postRequest.ContentType = "application/x-www-form-urlencoded;charset=big5"; |
17 | postRequest.ContentLength = data.Length; |
18 | Stream requestStream = postRequest.GetRequestStream(); |
19 | requestStream.Write(data, 0, data.Length); |
20 | requestStream.Close(); |
21 | }</STRING,STRING></STRING,STRING> |