Help with HTTP Post in C#

I don’t really know if this is a bug or if this is just me being stupid but I’m having a tough time posting a URL in C# on FiveM.

Problem: Everytime I run to post the URL, it freezes the then I have to timeout the Task for it to keep the server running. If I never timed out the task, it would just keep trying to “post” until I restarted the server. I’ve tested the URL I’m trying to post and it works just fine with an external website (haven’t tried C# on my PC yet).

My current code:

        private static async Task<string> PostAsync(string url, Dictionary<string, string> args)
        {
            Log.WriteLine("http");
            using (var client = new HttpClient())
            {
                Log.WriteLine("Content");
                var content = new FormUrlEncodedContent(args);
                Log.WriteLine("PostAsync");
                var res = await client.PostAsync(url, content);
                Log.WriteLine("ReadAsync");
                var resStr = await res.Content.ReadAsStringAsync();
                Log.WriteLine("Return");
                return resStr;
            }
        }

I have also tried legacy methods, WebClient, and Flurl.Http for posting the website, all freeze the system.
The log writes are just to know when it freezes and it freezes right after the “PostAsync” message.

If someone has knowledge of how I could do this within mono and a FiveM server that would be nice

My system specs (if important):
Ubuntu Server 17.10
4gb RAM
Unknown processor (VPS won’t tell me)
Hoster: OVH

Have you tried looking at this example? Working Http/Web Request Code-Snippet for C# Scripts

1 Like

I’ll take a look at it once I get back on my PC.

@Vespura
I’m trying to get it to work but everytime I try posting the server I get a NullReferenceException error along with it timing out.

Here is my code:

                Log.WriteLine("Serialization1");
                var info = new Dictionary<string, object>
                {
                    {"dump_json", JsonParser.Serialize(write2)},
                    {"code", code}
                };
                Log.WriteLine("Serialization2");
                var json = JsonParser.ToJson(info);

                Log.WriteLine("Task");
                Task<object> task = new ExportDictionary()[API.GetCurrentResourceName()].HttpRequest(POST_URL, "POST",
                    json, "[]");
                if (!task.Wait(7500))
                    throw new TimeoutException();

                var res = (string) task.Result;
                Log.WriteLine($"Result: {res}");

I tried removing all of the lines from the Response void inside of the http request script to see if that was causing the NullReferenceException but it wasn’t. Don’t really know what to do.

I haven’t really used it much myself either so your best try is to ask the creator of that snippet to help you out :confused: .

1 Like

Alright, I sent a personal message to the author. Thanks for the help :smile:

Fixed the issues by providing a wrapper class, I hope.