Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
adamcti opened this issue Sep 19, 2022 · 2 comments

Comments

@adamcti
Copy link

adamcti commented Sep 19, 2022

Hi I have the following code producing an error "The request message was already sent. Cannot send the same request message multiple times."

what is the fix because I assumed the code would work out of box...
`

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using System.Threading.Tasks;
using Medidata.MAuth.Core;
namespace Medidata
{
public class RequestExample
{

    static async Task Main(string[] args)
    {

       
        var mauthSigningHander = new MAuthSigningHandler(
            new MAuthSigningOptions()
            {
                ApplicationUuid = new Guid("670e052b-6858-4a34-9b18-4cb44ddfec86"),
                PrivateKey = "C:/some/location/on/drive/key.pem"
            }
          );
        var client = new HttpClient(mauthSigningHander);
        
        var request = new HttpRequestMessage(HttpMethod.Get, "https://www.imedidata.com/api/v2/study_groups.json");

        request.Headers.Add("Accept", "application/json");
        request.Headers.Add("Mcc-Version", "v2019-04-12");

        var response = await client.SendAsync(request);
        
        response = await client.SendAsync(request);

        response.EnsureSuccessStatusCode();

        Console.WriteLine("The Response body is the following:");
        Console.WriteLine($"{await response.Content.ReadAsStringAsync()}");

        Console.WriteLine("\nPress ANY key to exit");
        Console.ReadKey();
    }
}

}

`

@zczhi
Copy link

zczhi commented Nov 9, 2023

Hi, As you know, the error message mentions that the same parameter is used twice as the request parameter. This position is wrong in your sample code.

        ...
        var response = await client.SendAsync(request);
        
        response = await client.SendAsync(request);
        ...

@GoncharovMain
Copy link

GoncharovMain commented Jun 13, 2024

Attention! Not safe! To demonstrate internal implementation:

public static class HttpRequestMessageExtension
{
    public static void Reset(this HttpRequestMessage message)
    {
        Type type = message.GetType();

        /// From source code: https://github.com/microsoft/referencesource/blob/master/System/net/System/Net/Http/HttpRequestMessage.cs
        /// 
        /// private const int messageAlreadySent = 1; // signals that this message was already sent. 
        /// private const int messageNotYetSent = 0;
        // If this field is 0 (default), then the message wasn't sent by an HttpClient instance yet. If the field
        // value is 'messageSent', then the message was already sent and should not be sent again.
        /// private int sendStatus;
        
        /// But reflection find only _sendStatus field
        FieldInfo fieldStatusSend = type.GetField("_sendStatus", BindingFlags.NonPublic | BindingFlags.Instance);

        const int messageNotYetSent = 0;

        fieldStatusSend.SetValue(message, messageNotYetSent);
        /// Not disposing!
        // message.Dispose();
    }
}
static async Task Main(string[] args)
{
    ...
    var response = await client.SendAsync(request);
    request.Reset();
    response = await client.SendAsync(request);
    ...
}

Better use, for example, a property with expression-bodied property:

public class Entry
{
    private HttpMethod _method = HttpMethod.Get;
    private Uri _uri = new("https://www.imedidata.com/api/v2/study_groups.json");
    public HttpRequestMessage Request => new(_method , _uri)
    {
        Headers =
        {
            { "Accept", "application/json" },
            { "Mcc-Version", "v2019-04-12" },
        }
    }
}
static async Task Main(string[] args)
{
    ...
    Entry entry = new();
    var response = await client.SendAsync(entry.Request);
    response = await client.SendAsync(entry.Request);
    ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants