Skip to content

Commit

Permalink
为HttpClient模块提供POST方法的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
LanbingIce committed Sep 28, 2023
1 parent 12da5d9 commit 5ecdd51
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions IsaacSocket/Modules/HttpClientModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,36 +201,39 @@ internal override void ReceiveMemoryMessage(byte[] message)
{
request = new()
{
Content = new ByteArrayContent(body),
Method = action == ActionType.GET_REQUEST ? HttpMethod.Get : HttpMethod.Post,
RequestUri = new(url)
};
foreach (var kvp in headers)
{
request.Headers.Add(kvp.Key, kvp.Value);
try
{
request.Headers.Add(kvp.Key, kvp.Value);
}
catch (InvalidOperationException)
{
request.Content.Headers.Add(kvp.Key, kvp.Value);
}
}
}
catch (Exception e)
{
Callback(CallbackType.MEMORY_MESSAGE_GENERATED, SerializeFaulted(id, e.Message));
return;
}
switch (action)
{
case ActionType.GET_REQUEST:
request.Method = HttpMethod.Get;
Interlocked.Increment(ref taskCounter);
client.SendAsync(request, cancellationTokenSource.Token).ContinueWith(task =>
{
if (task.IsCompletedSuccessfully)
{
Callback(CallbackType.MEMORY_MESSAGE_GENERATED, SerializeResponse(id, task.Result));
}
else
{
Callback(CallbackType.MEMORY_MESSAGE_GENERATED, SerializeFaulted(id, task.Exception?.Message ?? ""));
}
Interlocked.Decrement(ref taskCounter);
});
break;
}
Interlocked.Increment(ref taskCounter);
client.SendAsync(request, cancellationTokenSource.Token).ContinueWith(task =>
{
if (task.IsCompletedSuccessfully)
{
Callback(CallbackType.MEMORY_MESSAGE_GENERATED, SerializeResponse(id, task.Result));
}
else
{
Callback(CallbackType.MEMORY_MESSAGE_GENERATED, SerializeFaulted(id, task.Exception?.Message ?? ""));
}
Interlocked.Decrement(ref taskCounter);
});
}
}

0 comments on commit 5ecdd51

Please sign in to comment.