Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send POST with form data in C#

Tags:

c#

I am trying to make a program that requests my website with a username, password, hardware ID and a key in POST.

I have this code here that should send a POST request to my website with that form data, but when it sends, my webserver reports back that it didn't recieve the POST data

try
            {
                string poststring = String.Format("username={0}&password={1}&key={2}&hwid={3}", Username, Password, "272453745345934756392485764589", GetHardwareID());
                HttpWebRequest httpRequest =
    (HttpWebRequest)WebRequest.Create("mywebsite");

                httpRequest.Method = "POST";
                httpRequest.ContentType = "application/x-www-form-urlencoded";

                byte[] bytedata = Encoding.UTF8.GetBytes(poststring);
                httpRequest.ContentLength = bytedata.Length;

                Stream requestStream = httpRequest.GetRequestStream();
                requestStream.Write(bytedata, 0, bytedata.Length);
                requestStream.Close();


                HttpWebResponse httpWebResponse =
                (HttpWebResponse)httpRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();

                StringBuilder sb = new StringBuilder();

                using (StreamReader reader =
                new StreamReader(responseStream, System.Text.Encoding.UTF8))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        sb.Append(line);
                    }
                }

                return sb.ToString();
            }
            catch (Exception Error)
            {
                return Error.ToString();
            }

If someone could help me, I would really appreciate it.

like image 255
KhrysusDev Avatar asked Jul 01 '20 14:07

KhrysusDev


1 Answers

As per HttpWebRequest documentation

We don't recommend that you use HttpWebRequest for new development. Instead, use the System.Net.Http.HttpClient class.

HttpClient contains only asynchronous API because Web requests needs awaiting. That's not good to freeze entire Application while it's pending response.

Thus, here's some async function to make POST request with HttpClient and send there some data.

First of all, create HttpClient seperately because

HttpClient is intended to be instantiated once per application, rather than per-use.

private static readonly HttpClient client = new HttpClient();

Then implement the method.

private async Task<string> PostHTTPRequestAsync(string url, Dictionary<string, string> data)
{
    using (HttpContent formContent = new FormUrlEncodedContent(data))
    {
        using (HttpResponseMessage response = await client.PostAsync(url, formContent).ConfigureAwait(false))
        {
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
        }
    }
}

Or C# 8.0

private async Task<string> PostHTTPRequestAsync(string url, Dictionary<string, string> data)
{
    using HttpContent formContent = new FormUrlEncodedContent(data);
    using HttpResponseMessage response = await client.PostAsync(url, formContent).ConfigureAwait(false);
    response.EnsureSuccessStatusCode();
    return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}

Looks easier than your code, right?

Caller async method will look like

private async Task MyMethodAsync()
{
    Dictionary<string, string> postData = new Dictionary<string, string>();
    postData.Add("message", "Hello World!");
    try
    {
        string result = await PostHTTPRequestAsync("http://example.org", postData);
        Console.WriteLine(result);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

If you're not familiar with async/await, it's time to say Hello.

like image 179
aepot Avatar answered Oct 19 '22 23:10

aepot