How to read/output response from the HTTP request The Next CEO of Stack OverflowWhat's the difference between a POST and a PUT HTTP REQUEST?HTTP GET request in JavaScript?Is an entity body allowed for an HTTP DELETE request?HTTP GET with request bodymaximum length of HTTP GET request?How to use java.net.URLConnection to fire and handle HTTP requestsHTTP response code for POST when resource already existsHow to make HTTP POST web requestHow is an HTTP POST request made in node.js?How are parameters sent in an HTTP POST request?
Grabbing quick drinks
What do "high sea" and "carry" mean in this sentence?
How to safely derail a train during transit?
What's the point of interval inversion?
How to get regions to plot as graphics
Shade part of a Venn diagram
What is the point of a new vote on May's deal when the indicative votes suggest she will not win?
How to be diplomatic in refusing to write code that breaches the privacy of our users
How to write the block matrix in LaTex?
How easy is it to start Magic from scratch?
What is the purpose of the Evocation wizard's Potent Cantrip feature?
Implement the Thanos sorting algorithm
Why doesn't a table tennis ball float on the surface? How do we calculate buoyancy here?
Is it okay to store user locations?
How to write papers efficiently when English isn't my first language?
Can I equip Skullclamp on a creature I am sacrificing?
Inappropriate reference requests from Journal reviewers
Term for the "extreme-extension" version of a straw man fallacy?
Text adventure game code
% symbol leads to superlong (forever?) compilations
Anatomically Correct Strange Women In Ponds Distributing Swords
Only print output after finding pattern
Why do remote companies require working in the US?
WOW air has ceased operation, can I get my tickets refunded?
How to read/output response from the HTTP request
The Next CEO of Stack OverflowWhat's the difference between a POST and a PUT HTTP REQUEST?HTTP GET request in JavaScript?Is an entity body allowed for an HTTP DELETE request?HTTP GET with request bodymaximum length of HTTP GET request?How to use java.net.URLConnection to fire and handle HTTP requestsHTTP response code for POST when resource already existsHow to make HTTP POST web requestHow is an HTTP POST request made in node.js?How are parameters sent in an HTTP POST request?
I send HTTP request to a webpage to insert or retrieve data.
This is my code:
string json = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
var response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", response, "test");
For this particular example; the website should return true or false.
But I want to read the response variable.
The DisplayAlert("test", response, "test");
show error. And this is because I am trying to read response outside of scope.
My question is how to read the response variable or output response variable on the page?
Edit
LoginModel user = new LoginModel();
user.email = email.Text;
user.password = password.Text;
;
string json = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
var response = client.PostAsync(
"https://scs.agsigns.co.uk/tasks/photoapi/login-photoapi/login-check.php",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", response, "test");
c# http
add a comment |
I send HTTP request to a webpage to insert or retrieve data.
This is my code:
string json = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
var response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", response, "test");
For this particular example; the website should return true or false.
But I want to read the response variable.
The DisplayAlert("test", response, "test");
show error. And this is because I am trying to read response outside of scope.
My question is how to read the response variable or output response variable on the page?
Edit
LoginModel user = new LoginModel();
user.email = email.Text;
user.password = password.Text;
;
string json = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
var response = client.PostAsync(
"https://scs.agsigns.co.uk/tasks/photoapi/login-photoapi/login-check.php",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", response, "test");
c# http
Moveresponse
outside ofusing
.HttpResponseMessage response; using(var client ...)
. Another thing is, you are not awaiting yourPostAsnyc()
call which will led to response being of typeTask<HttpResponseMessage>
– croxy
Mar 8 at 13:00
If I move response outside; Than I get red line under "client". See Edit original question
– James Hickling
Mar 8 at 13:05
You should only move the declaration ofresponse
outside of the using. Theclient.PostAsync()
call should stay inside of the using.
– croxy
Mar 8 at 13:07
docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/…
– demo
Mar 8 at 13:08
response.Content.ReadAsStringAsync()
– miechooy
Mar 8 at 13:14
add a comment |
I send HTTP request to a webpage to insert or retrieve data.
This is my code:
string json = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
var response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", response, "test");
For this particular example; the website should return true or false.
But I want to read the response variable.
The DisplayAlert("test", response, "test");
show error. And this is because I am trying to read response outside of scope.
My question is how to read the response variable or output response variable on the page?
Edit
LoginModel user = new LoginModel();
user.email = email.Text;
user.password = password.Text;
;
string json = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
var response = client.PostAsync(
"https://scs.agsigns.co.uk/tasks/photoapi/login-photoapi/login-check.php",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", response, "test");
c# http
I send HTTP request to a webpage to insert or retrieve data.
This is my code:
string json = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
var response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", response, "test");
For this particular example; the website should return true or false.
But I want to read the response variable.
The DisplayAlert("test", response, "test");
show error. And this is because I am trying to read response outside of scope.
My question is how to read the response variable or output response variable on the page?
Edit
LoginModel user = new LoginModel();
user.email = email.Text;
user.password = password.Text;
;
string json = JsonConvert.SerializeObject(user);
using (var client = new HttpClient())
var response = client.PostAsync(
"https://scs.agsigns.co.uk/tasks/photoapi/login-photoapi/login-check.php",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", response, "test");
c# http
c# http
edited Mar 8 at 13:06
James Hickling
asked Mar 8 at 12:59
James HicklingJames Hickling
65
65
Moveresponse
outside ofusing
.HttpResponseMessage response; using(var client ...)
. Another thing is, you are not awaiting yourPostAsnyc()
call which will led to response being of typeTask<HttpResponseMessage>
– croxy
Mar 8 at 13:00
If I move response outside; Than I get red line under "client". See Edit original question
– James Hickling
Mar 8 at 13:05
You should only move the declaration ofresponse
outside of the using. Theclient.PostAsync()
call should stay inside of the using.
– croxy
Mar 8 at 13:07
docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/…
– demo
Mar 8 at 13:08
response.Content.ReadAsStringAsync()
– miechooy
Mar 8 at 13:14
add a comment |
Moveresponse
outside ofusing
.HttpResponseMessage response; using(var client ...)
. Another thing is, you are not awaiting yourPostAsnyc()
call which will led to response being of typeTask<HttpResponseMessage>
– croxy
Mar 8 at 13:00
If I move response outside; Than I get red line under "client". See Edit original question
– James Hickling
Mar 8 at 13:05
You should only move the declaration ofresponse
outside of the using. Theclient.PostAsync()
call should stay inside of the using.
– croxy
Mar 8 at 13:07
docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/…
– demo
Mar 8 at 13:08
response.Content.ReadAsStringAsync()
– miechooy
Mar 8 at 13:14
Move
response
outside of using
. HttpResponseMessage response; using(var client ...)
. Another thing is, you are not awaiting your PostAsnyc()
call which will led to response being of type Task<HttpResponseMessage>
– croxy
Mar 8 at 13:00
Move
response
outside of using
. HttpResponseMessage response; using(var client ...)
. Another thing is, you are not awaiting your PostAsnyc()
call which will led to response being of type Task<HttpResponseMessage>
– croxy
Mar 8 at 13:00
If I move response outside; Than I get red line under "client". See Edit original question
– James Hickling
Mar 8 at 13:05
If I move response outside; Than I get red line under "client". See Edit original question
– James Hickling
Mar 8 at 13:05
You should only move the declaration of
response
outside of the using. The client.PostAsync()
call should stay inside of the using.– croxy
Mar 8 at 13:07
You should only move the declaration of
response
outside of the using. The client.PostAsync()
call should stay inside of the using.– croxy
Mar 8 at 13:07
docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/…
– demo
Mar 8 at 13:08
docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/…
– demo
Mar 8 at 13:08
response.Content.ReadAsStringAsync()
– miechooy
Mar 8 at 13:14
response.Content.ReadAsStringAsync()
– miechooy
Mar 8 at 13:14
add a comment |
2 Answers
2
active
oldest
votes
This gives you an error because you try to access a variable which is declared inside of a different scope. If you move the variable response
inside of the "method scope" the error will disappear:
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = await client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", await response.Content.ReadAsStringAsync(), "test");
Note the await
which I added just before client.PostAsync()
(You will find more infos about async/await in the docs).
To get the string representation of the response content you can use following method:
await response.Content.ReadAsStringAsync();
This will read the response content as string.
Thanks, @croxy. Still getting response conversion to string error. " Argument 2: cannot convert from 'System.Net.Http.HttpResponseMessage' to 'string'"
– James Hickling
Mar 8 at 13:19
add a comment |
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json").Result);
var body = response.Content.ReadAsStringAsync().Result;
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", body, "test");
should work, by moving the declaration of the variable outside the scope, and updating the value inside the call.
Thanks, it looks like it worked. Just threw another error - " cannot convert from 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'string'"...I believe I need to convert this into string; not sure what would be best practice to do so.
– James Hickling
Mar 8 at 13:13
Try the updated answer
– Ghanima
Mar 8 at 13:19
Thanks, @Crowxy and A Ghanima for all your help. I can see the response now. Thanks
– James Hickling
Mar 8 at 13:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063743%2fhow-to-read-output-response-from-the-http-request%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This gives you an error because you try to access a variable which is declared inside of a different scope. If you move the variable response
inside of the "method scope" the error will disappear:
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = await client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", await response.Content.ReadAsStringAsync(), "test");
Note the await
which I added just before client.PostAsync()
(You will find more infos about async/await in the docs).
To get the string representation of the response content you can use following method:
await response.Content.ReadAsStringAsync();
This will read the response content as string.
Thanks, @croxy. Still getting response conversion to string error. " Argument 2: cannot convert from 'System.Net.Http.HttpResponseMessage' to 'string'"
– James Hickling
Mar 8 at 13:19
add a comment |
This gives you an error because you try to access a variable which is declared inside of a different scope. If you move the variable response
inside of the "method scope" the error will disappear:
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = await client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", await response.Content.ReadAsStringAsync(), "test");
Note the await
which I added just before client.PostAsync()
(You will find more infos about async/await in the docs).
To get the string representation of the response content you can use following method:
await response.Content.ReadAsStringAsync();
This will read the response content as string.
Thanks, @croxy. Still getting response conversion to string error. " Argument 2: cannot convert from 'System.Net.Http.HttpResponseMessage' to 'string'"
– James Hickling
Mar 8 at 13:19
add a comment |
This gives you an error because you try to access a variable which is declared inside of a different scope. If you move the variable response
inside of the "method scope" the error will disappear:
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = await client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", await response.Content.ReadAsStringAsync(), "test");
Note the await
which I added just before client.PostAsync()
(You will find more infos about async/await in the docs).
To get the string representation of the response content you can use following method:
await response.Content.ReadAsStringAsync();
This will read the response content as string.
This gives you an error because you try to access a variable which is declared inside of a different scope. If you move the variable response
inside of the "method scope" the error will disappear:
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = await client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json"));
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", await response.Content.ReadAsStringAsync(), "test");
Note the await
which I added just before client.PostAsync()
(You will find more infos about async/await in the docs).
To get the string representation of the response content you can use following method:
await response.Content.ReadAsStringAsync();
This will read the response content as string.
edited Mar 8 at 13:20
answered Mar 8 at 13:11
croxycroxy
2,95072039
2,95072039
Thanks, @croxy. Still getting response conversion to string error. " Argument 2: cannot convert from 'System.Net.Http.HttpResponseMessage' to 'string'"
– James Hickling
Mar 8 at 13:19
add a comment |
Thanks, @croxy. Still getting response conversion to string error. " Argument 2: cannot convert from 'System.Net.Http.HttpResponseMessage' to 'string'"
– James Hickling
Mar 8 at 13:19
Thanks, @croxy. Still getting response conversion to string error. " Argument 2: cannot convert from 'System.Net.Http.HttpResponseMessage' to 'string'"
– James Hickling
Mar 8 at 13:19
Thanks, @croxy. Still getting response conversion to string error. " Argument 2: cannot convert from 'System.Net.Http.HttpResponseMessage' to 'string'"
– James Hickling
Mar 8 at 13:19
add a comment |
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json").Result);
var body = response.Content.ReadAsStringAsync().Result;
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", body, "test");
should work, by moving the declaration of the variable outside the scope, and updating the value inside the call.
Thanks, it looks like it worked. Just threw another error - " cannot convert from 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'string'"...I believe I need to convert this into string; not sure what would be best practice to do so.
– James Hickling
Mar 8 at 13:13
Try the updated answer
– Ghanima
Mar 8 at 13:19
Thanks, @Crowxy and A Ghanima for all your help. I can see the response now. Thanks
– James Hickling
Mar 8 at 13:24
add a comment |
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json").Result);
var body = response.Content.ReadAsStringAsync().Result;
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", body, "test");
should work, by moving the declaration of the variable outside the scope, and updating the value inside the call.
Thanks, it looks like it worked. Just threw another error - " cannot convert from 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'string'"...I believe I need to convert this into string; not sure what would be best practice to do so.
– James Hickling
Mar 8 at 13:13
Try the updated answer
– Ghanima
Mar 8 at 13:19
Thanks, @Crowxy and A Ghanima for all your help. I can see the response now. Thanks
– James Hickling
Mar 8 at 13:24
add a comment |
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json").Result);
var body = response.Content.ReadAsStringAsync().Result;
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", body, "test");
should work, by moving the declaration of the variable outside the scope, and updating the value inside the call.
string json = JsonConvert.SerializeObject(user);
HttpResponseMessage response;
using (var client = new HttpClient())
response = client.PostAsync(
"url",
new StringContent(json, Encoding.UTF8, "application/json").Result);
var body = response.Content.ReadAsStringAsync().Result;
DisplayAlert("Alert", json, "OK");
DisplayAlert("test", body, "test");
should work, by moving the declaration of the variable outside the scope, and updating the value inside the call.
edited Mar 8 at 13:19
answered Mar 8 at 13:02
GhanimaGhanima
5881216
5881216
Thanks, it looks like it worked. Just threw another error - " cannot convert from 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'string'"...I believe I need to convert this into string; not sure what would be best practice to do so.
– James Hickling
Mar 8 at 13:13
Try the updated answer
– Ghanima
Mar 8 at 13:19
Thanks, @Crowxy and A Ghanima for all your help. I can see the response now. Thanks
– James Hickling
Mar 8 at 13:24
add a comment |
Thanks, it looks like it worked. Just threw another error - " cannot convert from 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'string'"...I believe I need to convert this into string; not sure what would be best practice to do so.
– James Hickling
Mar 8 at 13:13
Try the updated answer
– Ghanima
Mar 8 at 13:19
Thanks, @Crowxy and A Ghanima for all your help. I can see the response now. Thanks
– James Hickling
Mar 8 at 13:24
Thanks, it looks like it worked. Just threw another error - " cannot convert from 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'string'"...I believe I need to convert this into string; not sure what would be best practice to do so.
– James Hickling
Mar 8 at 13:13
Thanks, it looks like it worked. Just threw another error - " cannot convert from 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'string'"...I believe I need to convert this into string; not sure what would be best practice to do so.
– James Hickling
Mar 8 at 13:13
Try the updated answer
– Ghanima
Mar 8 at 13:19
Try the updated answer
– Ghanima
Mar 8 at 13:19
Thanks, @Crowxy and A Ghanima for all your help. I can see the response now. Thanks
– James Hickling
Mar 8 at 13:24
Thanks, @Crowxy and A Ghanima for all your help. I can see the response now. Thanks
– James Hickling
Mar 8 at 13:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063743%2fhow-to-read-output-response-from-the-http-request%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Move
response
outside ofusing
.HttpResponseMessage response; using(var client ...)
. Another thing is, you are not awaiting yourPostAsnyc()
call which will led to response being of typeTask<HttpResponseMessage>
– croxy
Mar 8 at 13:00
If I move response outside; Than I get red line under "client". See Edit original question
– James Hickling
Mar 8 at 13:05
You should only move the declaration of
response
outside of the using. Theclient.PostAsync()
call should stay inside of the using.– croxy
Mar 8 at 13:07
docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/…
– demo
Mar 8 at 13:08
response.Content.ReadAsStringAsync()
– miechooy
Mar 8 at 13:14