If Method A posts as follows:
public void testPostAndGet()
{
using (var client = new HttpClient())
{
var uri = new Uri("https://localhost:44322/test");
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("email", "email@test.com")
};
var content = new FormUrlEncodedContent(pairs);
var responsePost = client.PostAsync(uri, content).Result;
if (responsePost.IsSuccessStatusCode)
{
}
}
}
To method B, which also returns a List
:
public async Task<List<EventItem>> test()
{
List<EventItem> items = new List<EventItem>();
NameValueCollection nvc = Request.Form;
string email = nvc["email"];
string test = "";
try
{
items = await eventsService.GetAllEvents(graphClient, email);
}
catch (ServiceException se)
{
}
return items;
}
How can I access the List
returned from method B from method A? ie How can I send a Post and Get in the same call to the same endpoint?
something like this will work:
public async Task<TResult> PostAsync<TResult, TInput>(string uriString, TInput payload = null) where TInput : class
{
var uri = new Uri(uriString);
using (var client = GetHttpClient())
{
var jsonContent = JsonConvert.SerializeObject(payload, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
HttpResponseMessage response = await client.PostAsync(uri, new StringContent(jsonContent, Encoding.UTF8, "application/json"));
if (response.StatusCode != HttpStatusCode.OK)
{
//Log.Error(response.ReasonPhrase);
return default(TResult);
}
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TResult>(json);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.