顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2025年6月12日 星期四

[筆記][DotNetCore] GetEncode950錯誤

 No data is available for encoding 950. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method

  • 解法:

        byte[] buffer = File.ReadAllBytes(filePath);
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

        var ansi = Encoding.GetEncoding(950);/
        ansi.GetString(buffer);
        

2025年1月14日 星期二

[筆記]瀏覽器可以正常取得JSON資料,但使用C#_HttpClient取資料,執行緩慢。

問題:瀏覽器可以正常取得JSON資料,但使用C# HttpClient取資料,執行緩慢。
ChatGPT:其中一種原因。可能有使用Proxy。
解法:將Proxy關閉。
HttpClientHandler handler = new HttpClientHandler
{
 Proxy = null,
 UseProxy = false
};
HttpClient client = new HttpClient(handler);


2024年7月11日 星期四

[筆記][C#]相對網址正式測式區轉換議題。


string Site="https://web.mydomain.com/demo/index.html";

string Site2="https://www.demo.com.tw/index.html";

string relateUrl = "Referee/Index.aspx?rid=123";//新網址

Uri siteUrl = new Uri(Site);//模擬Reuest.Url

Uri siteUrl2 = new Uri(Site2);

var baseUri = siteUrl;

var newUri = new Uri(baseUri,relateUrl); //透過基礎網址跟relate網址去產生正式網址

var baseUri2 = siteUrl2;

var newUri2 = new Uri(baseUri2,relateUrl);

Console.WriteLine(newUri.AbsoluteUri);

Console.WriteLine(newUri2.AbsoluteUri);

 

2020年8月5日 星期三

JsonToDataTable

參考:
  1. https://rextester.com/LTRMYJ47380
  2. https://gist.github.com/KyleShen/88ef81e7e279b376e2f26dc9469d7d2e
方法:透過Newtonsoft.Json套件傳換

    string jsonString= "[{\"id\":3,\"name\":\"peter\"}]";
    DataTable dt = (DataTable)JsonConvert.DeserializeObject(jsonString, (typeof(DataTable)));

注意點:
    以此案例所傳的jsonstring一定要是以"[" 開頭"]" 的字串。(array)