Created
February 25, 2014 15:02
-
-
Save hawx/9210536 to your computer and use it in GitHub Desktop.
ChunkyRequest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Xml.Serialization; | |
using ShiftIt.Http; | |
namespace Chunkyness | |
{ | |
public class ChunkyRequest | |
{ | |
private readonly IHttpClient _client; | |
private readonly IHttpRequest _request; | |
public ChunkyRequest(IHttpClient client, IHttpRequest request) | |
{ | |
_client = client; | |
_request = request; | |
} | |
public void Execute<T>(Action<T> callback) | |
{ | |
Execute(s => | |
{ | |
var reader = new StringReader(s); | |
var serialiser = new XmlSerializer(typeof(T)); | |
var obj = (T)serialiser.Deserialize(reader); | |
callback(obj); | |
}); | |
} | |
public void Execute(Action<string> callback) | |
{ | |
using (var result = _client.Request(_request)) | |
{ | |
var stream = result.RawBodyStream; | |
var reader = new StreamReader(stream); | |
while (true) | |
{ | |
var line = reader.ReadLine(); | |
if (line == null) | |
return; | |
var chunkSize = Convert.ToInt32(line.Trim(), 16); | |
if (chunkSize == 0) | |
return; | |
var buffer = new char[chunkSize]; | |
reader.Read(buffer, 0, chunkSize); | |
reader.ReadLine(); | |
var s = new string(buffer); | |
callback(s); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment