0
我正在使用Twitter流媒體API(C#)並定期獲取IOE異常(每12小時或類似的東西),而流閱讀器正試圖讀取JSON對象(的readLine)。請查看下面的代碼例外&。任何幫助將不勝感激。先謝謝你。Twitter流媒體過濾器API:定期獲取IOE異常
無法從傳輸連接讀取數據:連接嘗試失敗,因爲連接方在一段時間後沒有正確響應,或者建立連接失敗,因爲連接的主機未能響應。
string userName = "XXXX";
string password = "XXXX";
string url = @"https://stream.twitter.com/1.1/statuses/filter.json";
string parameters = "follow=" + userIds +"&track=" + keywords;
// Request and Response
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
StreamReader streamReader = null;
byte[] byteArray;
// Establishing connection with twitter filter.json
byteArray = Encoding.UTF8.GetBytes(parameters);
webRequest = (HttpWebRequest) WebRequest.Create(url);
webRequest.Credentials = new NetworkCredential(userName, password);
webRequest.Timeout = 100000;
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
byte[] postData = encode.GetBytes(parameters);
webRequest.ContentLength = postData.Length;
Stream twitterPost = webRequest.GetRequestStream();
twitterPost.Write(postData, 0, postData.Length);
twitterPost.Close();
webResponse = (HttpWebResponse)webRequest.GetResponse();
streamReader = new StreamReader(webResponse.GetResponseStream(), encode);
Stopwatch sw = new Stopwatch();
sw.Start();
while (sw.Elapsed < TimeSpan.FromDays(365))
{
String json = streamReader.ReadLine(); // [The program stop here. throws exception]
if (json == "")
{
continue;
}
Packet dataPacket = new Packet() { json = json, id = id++ };
ThreadPool.QueueUserWorkItem(new WaitCallback(Execute), dataPacket);
}
// Aborting the request and closing the stream
webRequest.Abort();
sw.Stop();
streamReader.Close();
streamReader = null;
webResponse.Close();
webResponse = null;