79522127

Date: 2025-03-20 07:09:53
Score: 1
Natty:
Report link

Firstly, thank you to @Alexander Zeitler. I am posting a complete example here of what worked for me. Alexander's code did not work for me out of the box as I had to adjust the form data to suit my purposes.


internal async Task PostIt()
{

    var username = "<Your username here>";
    
    var password = "<Your password here>";
    
    var baseAddress = new Uri("<Your POST Url here>");
    
    var cookieContainer = new CookieContainer();
    
    using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate })
    using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
    {
        client.DefaultRequestHeaders.Add("accept-encoding", "deflate");
        client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
        client.DefaultRequestHeaders.Add("Origin","<Origin URL here>");
        client.DefaultRequestHeaders.Referrer = new Uri("<Referrer URL here>");
        client.DefaultRequestHeaders.Add("connection", "keep-alive");
        client.Timeout = TimeSpan.FromMilliseconds(5000);
        // Add authorization headers here...
        var byteArray = new UTF8Encoding().GetBytes($"{username}:{password}");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
        
        /*
            Next comes the form data. I had to customize this to meet the requirements of the <baseAddress> server.
            
            Example: A Http debugger showed me when I was logging into <baseAddress>, the 'content' posted was as such:
            
            "login_username=<username here>&login_password=<password here>&redirect_url=<redirect URL here>&site=www"
            
            @Alexander Zeitler's code has thus been modified below.....
        
        */
    
        var formData = new List<KeyValuePair<string, string>>();
        formData.Add(new KeyValuePair<string, string>("login_username", username));
        formData.Add(new KeyValuePair<string, string>("login_password", password));
        formData.Add(new KeyValuePair<string, string>("redirect_url", "<redirect URL here>"));
        formData.Add(new KeyValuePair<string, string>("site", "www"));
    
        var request = new HttpRequestMessage(HttpMethod.Post, baseAddress);
    
        request.Content = new FormUrlEncodedContent(formData);
    
        var response = await client.SendAsync(request);
    
        if (response.IsSuccessStatusCode)
        {
        //do whatever........
        }
        else
        {
        
        }
    }
}
Reasons:
  • Blacklisted phrase (0.5): thank you
  • Blacklisted phrase (1): did not work
  • Whitelisted phrase (-1): worked for me
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Alexander
  • Low reputation (1):
Posted by: Seven7