【问题标题】:Twitter Streaming filter API: getting IOE exception on a regular basisTwitter 流过滤器 API:定期获取 IOE 异常
【发布时间】:2012-10-10 14:39:22
【问题描述】:

我正在使用 twitter 流 API (C#) 并定期(每 12 小时或类似的时间)获取 IOE 异常,而 Stream reader 正在尝试读取 JSON 对象(readLine)。请查看异常和下面的代码。任何帮助将不胜感激。提前谢谢你。

无法从传输连接中读取数据:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应。

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;

【问题讨论】:

    标签: c# twitter streaming


    【解决方案1】:

    听起来您的互联网连接中断或您与路由器的连接中断。

    在我的国家/地区,大多数 ISP 每天午夜刷新您的 IP 地址,这会导致连接中断几秒钟,也许您的 ISP 正在做类似的事情?

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2014-12-17
      • 2012-09-02
      相关资源
      最近更新 更多