Method ExtInternet.TryPost
Overload
Sends a POST request to the specified URL, and gets the response. Handles HTTP errors and exceptions.
public static bool TryPost(this HttpClient t, out HttpResponseMessage r, string url, HttpContent content, IEnumerable<string> headers = null, bool printError = false, bool dontWait = false, string auth = null, Action<HttpRequestMessage> also = null)
Parameters
| t (HttpClient) |
|
r (HttpResponseMessage)
Receives |
|
url (string)
URL. |
|
content (HttpContent)
Data to post. Usually web form data (see internet.formContent) or JSON (see internet.jsonContent). Can be |
|
headers (IEnumerable<string>)
|
|
printError (bool)
If failed, call print.warning. |
| dontWait (bool) |
|
auth (string)
String like |
|
also (Action<HttpRequestMessage>)
Can set more properties for the request. |
Returns
|
bool
|
Exceptions
|
UriFormatException
Invalid URL format. |
|
Exception
If headers used, exceptions of ExtInternet.AddMany. |
Examples
Post form data.
var content = internet.formContent(("name1", "value1"), ("name2", "value2"));
if (!internet.http.TryPost(out var r, "https://httpbin.org/anything", content, printError: true)) return;
print.it(r.Text());
Post object as JSON.
var v = new { a = "A", b = true };
if (!internet.http.TryPost(out var r, "https://httpbin.org/anything", internet.jsonContent(v), printError: true)) return;
print.it(r.Text());