我有一个ASP.NET MVC应用程序,它使用RestSharp连接到第三方,以便接收域名列表,然后在结果中稍后详细了解每个域。
我认为响应是在部分数据包中进行的,RestSharp正在等待所有数据包都被接收,然后以反序列化格式输出数据。
有问题的有效负载如下所示。 "头"首先填充,然后是我想立即返回视图的响应。其余数据的时间敏感度较低:
[
{"header":{"names":["test.1","test.2","test.3","test.4","test.5","test.6"]}}
,
{"name":"test.1","can":"transfer?"}
,
{"name":"test.2","can":"transfer?"}
,
{"name":"test.3","can":"transfer?"}
,
{"name":"test.4","can":"transfer?"}
,
{"name":"test.5","can":"transfer?"}
,
{"name":"test.6","can":"register"}
]
目前有两种执行实现,一种是同步,一种是异步:
public T ExecuteGetRequest<T>(RestRequest request) where T : class
{
try
{
IRestResponse response = _client.Execute(request);
if (response.ErrorException == null)
{
return JsonConvert.DeserializeObject<T>(response.Content);
}
return null;
}
catch (Exception ex)
{
return null;
}
}
public Task<T> ExecuteGetRequestAsync<T>(RestRequest request) where T : new()
{
try
{
var source = new TaskCompletionSource<T>();
_client.ExecuteAsync<T>(request, (response) => { source.SetResult(response.Data); });
return source.Task;
}
catch (Exception ex)
{
return null;
}
}
应用程序有一个本地API调用,使用async实现搜索,如下所示:
public async Task<DomainSearchResults> DomainSearchAsync(string domain)
{
var request = _client.CreateRequest("domain-search/{domain}", Method.GET);
request.AddUrlSegment("domain", domain);
var response = await _client.ExecuteGetRequestAsync<List<DomainSearch>>(request);
return new DomainSearchResults(response);
}
然后根据响应并向客户提供相关的搜索结果。
这种方法很好,因为当第三方发送了所有数据时,该对象将返回到视图并相应地填充。但是,请求的完整完成最多可能需要20秒,这对用户来说并不是特别有用。
有没有一种方法可以调整ExecuteGetRequestAsync,以便在收到完整响应之前开始将不完整的响应发送回调用视图?
我最初的尝试看起来很像这样:
public Task<T> ExecuteGetRequestAsyncIncomplete<T>(RestRequest request) where T : new()
{
try
{
var source = new TaskCompletionSource<T>();
_client.ExecuteAsync<T>(request, (response) =>
{
source.SetResult(response.Data);
if (response.StatusCode == HttpStatusCode.PartialContent)
{
// Somehow return part of this response
}
});
return source.Task;
}
catch (Exception ex)
{
return null;
}
}
更新
从@Evk的答案开始,这里返回部分结果的新调用是什么样的,特别针对这种情况:
public async Task<T> ExecuteGetRequestPartial<T>(RestRequest request) where T : new()
{
try
{
var source = new TaskCompletionSource<T>();
request.ResponseWriter = (st) => {
using (var reader = new StreamReader(st))
{
var sb = new StringBuilder();
// read response 100 chars at a time
char[] buffer = new char[1];
int read = 0;
while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
{
sb.Append(buffer, 0, read);
// now here you have your partial response
// you need to somehow parse it and feed to your view
// note that you should wait until you get some meaningful part, like first "header" element
// for example at some point there might be ["header":{"names":["te < partial response
if (sb.ToString().Contains("header") && sb.ToString().EndsWith("}"))
{
sb.Append("}]");
source.SetResult(JsonConvert.DeserializeObject<T>(sb.ToString()));
return;
}
}
// at this point you have full response in sb
}
};
await _client.ExecuteGetTaskAsync<T>(request);
return await source.Task;
}
catch (Exception ex)
{
return default(T);
}
}
简而言之,缓冲区已减少为1个字符,以便我们知道字符串中的位置。然后,为了将部分结果转换为有效的JSON,我们检查对象的结尾,然后通过手动添加额外的&#34;}]&#34;来关闭它。结果并返回。
好人Evk!
答案 0 :(得分:1)
首先,http状态“Partial Content”与您的案例无关。它用于响应具有Range标头的请求。
您需要的是读取响应流,而不是等待传递和反序列化的完整响应。使用常规HttpWebRequest更容易做到这一点,但如果你想使用RestSharp,它也是可能的。请注意,您必须手动反序列化部分(因此,无效)json。这是一幅草图:
public static Task<T> ExecuteGetRequestAsync<T>(RestRequest request) where T : new() {
var client = new RestClient("http://google.com");
try {
var source = new TaskCompletionSource<T>();
request.ResponseWriter = (st) => {
using (var reader = new StreamReader(st)) {
var sb = new StringBuilder();
// read response 100 chars at a time
char[] buffer = new char[100];
int read = 0;
while ((read = reader.Read(buffer, 0, buffer.Length)) > 0) {
sb.Append(buffer, 0, read);
// now here you have your partial response
// you need to somehow parse it and feed to your view
// note that you should wait until you get some meaningful part, like first "header" element
// for example at some point there might be ["header":{"names":["te < partial response
}
// at this point you have full response in sb
}
};
client.ExecuteAsync<T>(request, null);
return source.Task;
}
catch (Exception ex) {
return null;
}
}