Twitter ストリーミング API (C#) を使用しており、ストリーム リーダーが JSON オブジェクト (readLine) を読み込もうとしている間、定期的に (12 時間ごとなどに) 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;