我使用以下代码:
protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
UriBuilder forwardUri = new UriBuilder(request.RequestUri);
//remove the proxy port and replace with an Http port
forwardUri.Port = 1671;
//send it on to the requested URL
request.RequestUri = forwardUri.Uri;
HttpClient client = new HttpClient();
if (request.Method == HttpMethod.Get)
request.Content = null;
try
{
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if (response.StatusCode == HttpStatusCode.OK)
{
ManageOnline(response.RequestMessage);
}
return response;
}
catch (Exception ex)
{
throw;
}
}
问题发生在ManageOnline
方法中:
private void ManageOnline(HttpRequestMessage request)
{
switch (request.RequestUri.PathAndQuery)
{
case @"/token":
{
//let's get Content Body contents here
string jsonContent = request.Content.ReadAsStringAsync().Result;
_cacheHandler.AddUser(jsonContent);
break;
}
default:
break;
}
}
我收到 request.Content.ReadAsStringAsync()&#39;抛出类型&#39; System.ObjectDisposedException&#39;
的例外但是,如果我在ManageOnline(response.RequestMessage);
运行之前运行var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
。
无论如何要克服这个问题吗?
非常感谢