Here's an example of how to make an HTTP POST request with a custom body as form-urlencoded in C# using the HttpWebRequest
class:
main.cs797 chars24 lines
In this example, we set the url
variable to the API endpoint that we want to send our HTTP request to. We then create a HttpWebRequest
object and set its properties: Method
to POST
, ContentType
to application/x-www-form-urlencoded
, and the ContentLength
to the length of our formData
string.
Next, we get the request stream, convert our formData
to a byte array, and write it to the request stream.
We then send the HTTP POST request to the API endpoint, wait for the response, and get the response body using a StreamReader
. Finally, we print the response body to the console.
gistlibby LogSnag