Drahcir
Drahcir

Reputation: 11972

Re-Send HttpRequestMessage - Exception

I want to send the exact same request more than once, for example:

HttpClient client = new HttpClient();
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, "http://example.com");

await client.SendAsync(req, HttpCompletionOption.ResponseContentRead);
await client.SendAsync(req, HttpCompletionOption.ResponseContentRead);

Sending the request for a second time will throw an exception with the message:

The request message was already sent. Cannot send the same request message multiple times.

Is there a way to "clone" the request so that I can send again?

My real code has more variables set on the HttpRequestMessage than in the example above, variables like headers and request method.

Upvotes: 35

Views: 29911

Answers (5)

desautelsj
desautelsj

Reputation: 3725

Here is an improvement to the extension method proposed by @drahcir. The improvement is to ensure the content of the request is cloned as well as the request itself:

public static HttpRequestMessage Clone(this HttpRequestMessage request)
{
    var clone = new HttpRequestMessage(request.Method, request.RequestUri)
    {
        Content = request.Content.Clone(),
        Version = request.Version
    };
    foreach (KeyValuePair<string, object> prop in request.Properties)
    {
        clone.Properties.Add(prop);
    }
    foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
    {
        clone.Headers.TryAddWithoutValidation(header.Key, header.Value);
    }

    return clone;
}

public static HttpContent Clone(this HttpContent content)
{
    if (content == null) return null;

    var ms = new MemoryStream();
    content.CopyToAsync(ms).Wait();
    ms.Position = 0;

    var clone = new StreamContent(ms);
    foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers)
    {
        clone.Headers.Add(header.Key, header.Value);
    }
    return clone;
}

Edit 05/02/18: here's Async version

public static async Task<HttpRequestMessage> CloneAsync(this HttpRequestMessage request)
{
    var clone = new HttpRequestMessage(request.Method, request.RequestUri)
    {
        Content = await request.Content.CloneAsync().ConfigureAwait(false),
        Version = request.Version
    };
    foreach (KeyValuePair<string, object> prop in request.Properties)
    {
        clone.Properties.Add(prop);
    }
    foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
    {
        clone.Headers.TryAddWithoutValidation(header.Key, header.Value);
    }

    return clone;
}

public static async Task<HttpContent> CloneAsync(this HttpContent content)
{
    if (content == null) return null;

    var ms = new MemoryStream();
    await content.CopyToAsync(ms).ConfigureAwait(false);
    ms.Position = 0;

    var clone = new StreamContent(ms);
    foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers)
    {
        clone.Headers.Add(header.Key, header.Value);
    }
    return clone;
}

Upvotes: 22

Ricky
Ricky

Reputation: 10761

I have similar problem and resolved it in a hack way, reflection.

Thanks for open source! By reading the source code, it turns out there's a private field _sendStatus in HttpRequestMessage class, what I did is to reset it to 0 before reusing the request message. It works in .NET Core and I wish Microsoft would not rename or remove it for ever. :p

// using System.Reflection;
// using System.Net.Http;
// private const string SEND_STATUS_FIELD_NAME = "_sendStatus";
private void ResetSendStatus(HttpRequestMessage request)
{
    TypeInfo requestType = request.GetType().GetTypeInfo();
    FieldInfo sendStatusField = requestType.GetField(SEND_STATUS_FIELD_NAME, BindingFlags.Instance | BindingFlags.NonPublic);
    if (sendStatusField != null)
        sendStatusField.SetValue(request, 0);
    else
        throw new Exception($"Failed to hack HttpRequestMessage, {SEND_STATUS_FIELD_NAME} doesn't exist.");
}

Upvotes: 8

Nick
Nick

Reputation: 6618

I am passing around an instance of Func<HttpRequestMessage> instead of an instance of HttpRequestMessage. The func points to a factory method so I get a brand new message each time it is called instead of re-using.

Upvotes: 16

Drahcir
Drahcir

Reputation: 11972

I wrote the following extension method to clone the request.

public static HttpRequestMessage Clone(this HttpRequestMessage req)
{
    HttpRequestMessage clone = new HttpRequestMessage(req.Method, req.RequestUri);

    clone.Content = req.Content;
    clone.Version = req.Version;

    foreach (KeyValuePair<string, object> prop in req.Properties)
    {
        clone.Properties.Add(prop);
    }

    foreach (KeyValuePair<string, IEnumerable<string>> header in req.Headers)
    {
        clone.Headers.TryAddWithoutValidation(header.Key, header.Value);
    }

    return clone;
}

Upvotes: 24

Fred Deschenes
Fred Deschenes

Reputation: 71

AFAIK, HttpClient is just a wrapper around 'HttpWebRequest's, which use streams to send/receeive data, making it impossible to re-use the request, although it should be pretty simple to juste clone it/make this in a loop.

Upvotes: 0

Related Questions