C
C#2y ago
zandose

❔ HTTP code not returning expected result.

So I have the following code
FormUrlEncodedContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Test1", "Test2") });
Task<HttpResponseMessage> result = client.PostAsync(URL, content);
Task<string> resultContent = result.Result.Content.ReadAsStringAsync();
Debug.Log(resultContent.Result);
FormUrlEncodedContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Test1", "Test2") });
Task<HttpResponseMessage> result = client.PostAsync(URL, content);
Task<string> resultContent = result.Result.Content.ReadAsStringAsync();
Debug.Log(resultContent.Result);
Which should send the following http://zandose1.centralus.cloudapp.azure.com/test.php?Test1=Test2 and then I have some PHP code which should return Test1=Test2. If I put http://zandose1.centralus.cloudapp.azure.com/test.php?Test1=Test2 into a browser it works fine but with the C# code it doesn't get the anything.
15 Replies
Angius
Angius2y ago
You don't seem to ever be awaiting anything
zandose
zandoseOP2y ago
I was having issues writing the code with awaiting so I got rid of them for testing. Is it required to make it work?
Angius
Angius2y ago
It might be the cause of the issue, it might not be But it's best to eliminate things that go against the proper usage first, makes finding actual errors easier
zandose
zandoseOP2y ago
alright I'll try rewriting it with awaits
Angius
Angius2y ago
So
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Test1", "Test2") });
var result = await client.PostAsync(URL, content);
var resultContent = await result.Content.ReadAsStringAsync();
Debug.Log(resultContent);
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Test1", "Test2") });
var result = await client.PostAsync(URL, content);
var resultContent = await result.Content.ReadAsStringAsync();
Debug.Log(resultContent);
Axiss
Axiss2y ago
I'm not familiar with PHP but in the C# code you are POSTing the form data and in the browser you are doing a GET request. How are you reading the values in your PHP code?
zandose
zandoseOP2y ago
Thanks and done. Same result. Basically the PHP code takes the URL and returns it
echo "test | " . $_SERVER['QUERY_STRING'];
echo "test | " . $_SERVER['QUERY_STRING'];
but it's only returning the test part.
Angius
Angius2y ago
uh
Axiss
Axiss2y ago
That's the issue. The query string is everything after the ?. In a POST the data is sent as the body of the request.
Angius
Angius2y ago
You never send a query param named QUERY_STRING You send one named Test1 with the value Test2
zandose
zandoseOP2y ago
Hmm
Angius
Angius2y ago
Also, get those values from the $_GET superglobal Rule of thumb: don't use $_SERVER $_GET for query params, $_POST for form body
zandose
zandoseOP2y ago
I was just testing by returning everything. I haven't made the PHP correct and secure yet. OK, so this works
var result = await client.GetAsync("http://zandose1.centralus.cloudapp.azure.com/test.php?Test1=Test2");
var resultContent = await result.Content.ReadAsStringAsync();
Debug.Log(resultContent);
var result = await client.GetAsync("http://zandose1.centralus.cloudapp.azure.com/test.php?Test1=Test2");
var resultContent = await result.Content.ReadAsStringAsync();
Debug.Log(resultContent);
Just need to figure out how to add in the
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Test1", "Test2") });
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Test1", "Test2") });
part
Angius
Angius2y ago
That's something you would read from $_POST $_POST['Test1'] to be precise
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server