C
C#β€’2y ago
Steff

HTTP Post query

I am currently learning the post query using HTTP from the https://ptsv2.com page. My question is whether what I get in my textbox makes sense as far as it goes, or whether I've just written garbage after all. so, this was the code
try
{
using (var client = new HttpClient())
{
var urlString = new Uri("https://ptsv2.com");
var newToilet = new HeadersMain();
var newToiletJson = JsonConvert.SerializeObject(newToilet);
var load = new StringContent(newToiletJson, Encoding.UTF8, "application/json");
var result = client.PostAsync("https://ptsv2.com/t/" + textBox1.Text + "/post", load).Result;
richTextBox1.Text = result.ToString();
}
}
try
{
using (var client = new HttpClient())
{
var urlString = new Uri("https://ptsv2.com");
var newToilet = new HeadersMain();
var newToiletJson = JsonConvert.SerializeObject(newToilet);
var load = new StringContent(newToiletJson, Encoding.UTF8, "application/json");
var result = client.PostAsync("https://ptsv2.com/t/" + textBox1.Text + "/post", load).Result;
richTextBox1.Text = result.ToString();
}
}
16 Replies
Steff
Steffβ€’2y ago
and this is the HeadersMain:
public class HeadersMain
{
public string[] Accept { get; set; }
public string[] ContentLength { get; set; }
public string[] ContentType { get; set; }
public string[] Forwarded { get; set; }
public string[] Traceparent { get; set; }
public string[] UserAgent { get; set; }
public string[] XCloudTraceContext { get; set; }
public string[] XForwardedFor { get; set; }
public string[] XForwardedProto { get; set; }
public string[] XGoogleAppsMetadata { get; set; }
public string[] XGoogleServerlessNodeEnvoyNopod { get; set; }
}
public class HeadersMain
{
public string[] Accept { get; set; }
public string[] ContentLength { get; set; }
public string[] ContentType { get; set; }
public string[] Forwarded { get; set; }
public string[] Traceparent { get; set; }
public string[] UserAgent { get; set; }
public string[] XCloudTraceContext { get; set; }
public string[] XForwardedFor { get; set; }
public string[] XForwardedProto { get; set; }
public string[] XGoogleAppsMetadata { get; set; }
public string[] XGoogleServerlessNodeEnvoyNopod { get; set; }
}
Cisien
Cisienβ€’2y ago
you're just dumping the response, you're not going to get much interesting from that. You need to read the body to see what the response data was await request.Content.ReadAsStringAsync()
Steff
Steffβ€’2y ago
oh, and more not?
Cisien
Cisienβ€’2y ago
what?
Steff
Steffβ€’2y ago
So, when I compare the input from the program with the input from the page, the same thing comes out. (I'm really new to this, that's why I get such stupid statements).
undisputed world champions
rn you are just outputting the header data of the response (result.ToString()) which looks good because it says "StatusCode: 200, ReasonPhrase: 'OK'" but it also says "Content-Type: text/plain" and "Content-Length: 41", which means the response also has a 41 bytes long-plain text content you can get the string out of the content by calling await request.Content.ReadAsStringAsync() (as Cisien pointed out) and output it too, to see what it says
Steff
Steffβ€’2y ago
Yeah, I already did that.
var result = client.PostAsync("https://ptsv2.com/t/" + textBox1.Text + "/post", load).Result.Content.ReadAsStringAsync().Result;
var result = client.PostAsync("https://ptsv2.com/t/" + textBox1.Text + "/post", load).Result.Content.ReadAsStringAsync().Result;
undisputed world champions
ok, but you shouldnt just call .Result on Tasks properly await them πŸ˜‰ $async
MODiX
MODiXβ€’2y ago
For an introduction to async and await in C#, https://blog.stephencleary.com/2012/02/async-and-await.html
Async and Await
Most people have already heard about the new β€œasync” and β€œawait” functionality coming in Visual Studio 11. This is Yet Another Introductory Post.
Steff
Steffβ€’2y ago
oh Wait
Cisien
Cisienβ€’2y ago
no, await πŸ™‚
Steff
Steffβ€’2y ago
yes, await so?
var result = await client.PostAsync("https://ptsv2.com/t/" + textBox1.Text + "/post", load).Result.Content.ReadAsStringAsync();
var result = await client.PostAsync("https://ptsv2.com/t/" + textBox1.Text + "/post", load).Result.Content.ReadAsStringAsync();
Cisien
Cisienβ€’2y ago
don't chain those you're still calling Result on the Post var result = await client.PostAsync(...); var body = await result.Content.ReadAsStringAsync();
Steff
Steffβ€’2y ago
Thanks ! ❀️
undisputed world champions
probably wanna check result.StatusCode before you read the body, too πŸ˜‰ or call result.EnsureSucessStatusCode(), it will throw an exception, if the StatusCode does not indicate success πŸ˜‰
Steff
Steffβ€’2y ago
oh, thanks πŸ™‚