C# Reading “chunked” response with HttpWebResponse [ REST API chunked C# ]
C# Reading “chunked” response with HttpWebResponse [ REST API chunked C# ]
資料來源: https://stackoverflow.com/questions/16998/reading-chunked-response-with-httpwebresponse/15497373
GITHUB: https://github.com/jash-git/CS-Reading-REST-API-chunked
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Cache; using System.IO; using System.Threading; namespace CS_HttpWebResponse_chunked_thread_Timer_show_data { public partial class Form1 : Form { public static string m_StrNewData; public static string m_StrOldData; public Thread m_Thread; public static void Thread_fun(object arg) { RESTfulAPI_getchunked("http://192.168.1.196:24410/syris/sydm/events?events_type=0"); } public static void RESTfulAPI_getchunked(String url) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //-- //定義此req的緩存策略 //https://msdn.microsoft.com/zh-tw/library/system.net.webrequest.cachepolicy(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); request.CachePolicy = noCachePolicy; //-- request.Method = "GET";//request.Method = "POST"; //request.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) { encoding = "UTF-8"; //預設編碼 } //--- //add 2017/12/20 //Reading “chunked” response with HttpWebResponse - https://stackoverflow.com/questions/16998/reading-chunked-response-with-httpwebresponse StringBuilder sb = new StringBuilder(); Byte[] buf = new byte[8192]; Stream resStream = response.GetResponseStream(); string tmpString = null; int count = 0; do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tmpString = Encoding.UTF8.GetString(buf, 0, count); m_StrNewData = tmpString; } Thread.Sleep(10); } while (count > 0); response.Close(); } catch { } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { m_StrNewData = "wait..."; m_StrOldData = ""; timer1.Interval = 100; timer1.Enabled = true; m_Thread = new Thread(Thread_fun); m_Thread.Start(); } private void timer1_Tick(object sender, EventArgs e) { if (m_StrNewData != m_StrOldData) { m_StrOldData = m_StrNewData; richTextBox1.AppendText(m_StrOldData); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (m_Thread.IsAlive)//關閉執行序 { timer1.Enabled = false; m_Thread.Abort(); } } } }
<!-- HTTP chunked [分塊傳輸編碼] 資料來源:https://zh.wikipedia.org/wiki/%E5%88%86%E5%9D%97%E4%BC%A0%E8%BE%93%E7%BC%96%E7%A0%81 應答需要以0長度的塊( "0\r\n\r\n".)結束。 -->