To my surprise, I can't do anything nearly as simple as this, from what I can tell, in the .NET BCL:
byte[] response = Http.Post
(
url: "http://dork.com/service",
contentType: "application/x-www-form-urlencoded",
contentLength: 32,
content: "home=Cosby&favorite+flavor=flies"
);
This hypothetical code above makes an HTTP POST, with data, and returns the response from a Post
method on a static class Http
.
Since we're left without something this easy, what's the next best solution?
How do I send an HTTP POST with data AND get the response's content?
I know this is an old thread, but hope it helps some one.
Use WebRequest. From Scott Hanselman:
You will need these includes:
If you're insistent on using a static method/class:
Then simply:
Using HttpClient: as far as Windows 8 app development concerns, I came across this.
Given other answers are a few years old, currently here are my thoughts that may be helpful:
Simplest way
A More Practical Example
Often we are dealing with known types and JSON, so you can further extend this idea with any number of implementations, such as:
An example of how this could be called: