作者ansem (DoubleA)
看板C_Sharp
標題[問題] 無法使用AddHeader於ResClient取得資料
時間Thu Jan 19 17:35:50 2023
各位好,我目前遇到的問題是我想去證交所抓信用交易資料
使用以下的程式碼
string url = "
https://wwwc.twse.com.tw/zh/exchangeReport/MI_MARGN";
var client = new RestClient(url);
var request = new RestRequest("",Method.Get);
request.AddHeader("Accept", "*/*");
request.AddHeader("Accept-Encoding", "gzip, deflate, br");
request.AddHeader("Accept-Language",
"zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Content-Length", "42");
request.AddHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
request.AddHeader("Cookie", "_ga=_ga=GA1.3.228088691.1674099947;
_gid=GA1.3.1638277859.1674099947;
JSESSIONID=E0D3F21B26722058423036EA82A061FE; _gat=1");
request.AddHeader("Host", "wwwc.twse.com.tw");
request.AddHeader("Origin", "
https://wwwc.twse.com.tw");
request.AddHeader("Referer",
"
https://wwwc.twse.com.tw/zh/page/trading/exchange/MI_MARGN.html");
request.AddHeader("sec-ch-ua", "'Not_A Brand';v='99', 'Google
Chrome';v='109', 'Chromium';v='109'");
request.AddHeader("sec-ch-ua-mobile", "?0");
request.AddHeader("sec-ch-ua-platform", "'Windows'");
request.AddHeader("Sec-Fetch-Dest", "empty");
request.AddHeader("Sec-Fetch-Mode", "cors");
request.AddHeader("Sec-Fetch-Site", "same-origin");
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0;
Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0
Safari/537.36");
request.AddHeader("X-Requested-With", "XMLHttpRequest");
RestResponse response = client.Execute(request);
AddInfo(response.Content.ToString());
但是會一直出現回傳是null的狀態,但是我使用開發人員工具來看這是我在取得分類項目"全部"的需求header,請問這裡面是否有我漏掉的部分呢?
感謝各位的解惑
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.73.197.230 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_Sharp/M.1674120953.A.E8F.html
1F:推 nicetw20xx: 有response code嗎 01/25 17:54
2F:→ nicetw20xx: 另外一樣的request用postman打會有結果嗎 01/25 17:54
3F:→ ssccg: 明明是GET為什麼會有Content-Length、Content-Type? 01/26 23:51
4F:→ ssccg: 你知不知道Header的意義啊... 01/26 23:51
5F:→ ssccg: 說不定是對方server還在等你說好的42個bytes在哪呢 01/26 23:52
7F:→ ssccg: 這網址直接開都有東西了,根本不用那麼多Header 01/26 23:52