Hi,
I’m using a Fez Panda II version 4.1.5.0.
I am running the HttpWebRequest agains twitter for my ThermalTweeter project. The webrequest stays empty many times (but not always…). Anyone any idea what could be causing this?
When there is a result I get this:
{“completed_in”:0.022,“max_id”:98851831127613440,“max_id_str”:“98851831127613440”,“next_page”:“?page=2&max_id=98851831127613440&q=bieber&rpp=1”,“page”…
When there is no result, the debug just prints .
I already trimmed down ByteData and CharDate to prevent memory errors. I also query one tweet at a time to prevent overflowing the buffer. I’m also sure I’m not hammering search.twitter.com…
Any idea?
Merijn
using System;
using System.IO;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.NetworkInformation;
namespace ThermalTweeter
{
public class Program
{
static Thread thread;
static bool running;
//static UInt64 LastTwitterId, LastTwitterIdOld;
public static void Main()
{
// Enable the Ethernet
WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true);
Dhcp.EnableDhcp(new byte[] { 0x00, 0x26, 0x1C, 0x7B, 0x29, 0xE8 }, "tt");
// Show we've started by blinking the LED
int count = 0;
bool ledState = false;
OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);
while (count < 20)
{
ledState = !ledState;
led.Write(ledState);
count++;
Thread.Sleep(100);
}
// Begin reading twitter
running = true;
thread = new Thread(MainLoop);
thread.Priority = ThreadPriority.Highest;
thread.Start();
}
public static void MainLoop()
{
string result;
while (running)
{
result = GetTweets("http://search.twitter.com/search.json?q=bieber&page=1&result_type=recent&rpp=1&show_user=true&since_id=");
Debug.Print(result); //prints null all the time!!!
// first getting some results, then pagination, remembering last id, etc, etc
Thread.Sleep(30000);
}
}
public static string GetTweets(string url)
{
string result = null;
// Create an HTTP Web request.
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
// Set request.KeepAlive to use a persistent connection.
request.KeepAlive = false;
// Get a response from the server.
WebResponse resp = null;
try
{
resp = request.GetResponse();
}
catch (Exception e)
{
Debug.Print("Exception in HttpWebRequest.GetResponse(): " + e.ToString());
}
// Get the network response stream to read the page data.
if (resp != null)
{
Stream respStream = resp.GetResponseStream();
Debug.GC(true);
Thread.Sleep(1000);
byte[] byteData = new byte[1596]; //trimmed down for FEZ Panda II
char[] charData = new char[1596];
int bytesRead = 0;
Decoder UTF8decoder = System.Text.Encoding.UTF8.GetDecoder();
int totalBytes = 0;
// allow 5 seconds for reading the stream
respStream.ReadTimeout = 5000;
// If we know the content length, read exactly that amount of
// data; otherwise, read until there is nothing left to read.
if (resp.ContentLength != -1)
{
for (int dataRem = (int)resp.ContentLength; dataRem > 0; )
{
Thread.Sleep(500);
bytesRead = respStream.Read(byteData, 0, byteData.Length);
if (bytesRead == 0)
{
Debug.Print("Error: Received " + (resp.ContentLength - dataRem) + " Out of " + resp.ContentLength);
break;
}
dataRem -= bytesRead;
// Convert from bytes to chars, and add to the page string.
int byteUsed, charUsed;
bool completed = false;
totalBytes += bytesRead;
UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0, bytesRead, true, out byteUsed, out charUsed, out completed);
result = result + new String(charData, 0, charUsed);
}
result = new String(System.Text.Encoding.UTF8.GetChars(byteData));
}
else
{
// Read until the end of the data is reached.
while (true)
{
// If the Read method times out, it throws an exception,
// which is expected for Keep-Alive streams because the
// connection isn't terminated.
try
{
Thread.Sleep(500);
bytesRead =
respStream.Read(byteData, 0, byteData.Length);
}
catch (Exception)
{
bytesRead = 0;
}
// Zero bytes indicates the connection has been closed by the server.
if (bytesRead == 0)
break;
int byteUsed, charUsed;
bool completed = false;
totalBytes += bytesRead;
UTF8decoder.Convert(byteData, 0, bytesRead, charData, 0, bytesRead, true, out byteUsed, out charUsed, out completed);
result = result + new String(charData, 0, charUsed);
}
}
// Close the response stream. For Keep-Alive streams, the
// stream will remain open and will be pushed into the unused
// stream list.
resp.Close();
}
return result;
}
}
}