HttpClient c#- how to get dynamic content data? - c#

For example, i need to get price values from https://www.futbin.com/22/sales/415/erling-haaland?platform=pc, that are located on the 'sales-inner' table.
The problem is that HTTP Response returns results without loaded prices.
HttpResponseMessage response = await client.GetAsync("https://www.futbin.com/22/sales/415/?platform=pc");
response.EnsureSuccessStatusCode();
string responseString = await response.Content.ReadAsStringAsync();
How to get these data?

The URL you have mentioned in the question renders the view. To get the actual data you need to check the below URLs. Please note that I have got the below URLs from the debugger window but you can check docs if APIs are already provided.
https://www.futbin.com/22/getPlayerSales?resourceId=239085&platform=pc
https://www.futbin.com/getPlayerChart?type=live-sales&resourceId=239085&platform=pc
public async Task Main()
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(#"https://www.futbin.com/22/getPlayerSales?resourceId=239085&platform=pc");
response.EnsureSuccessStatusCode();
string responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}

You are getting html without results because they are loading the data when the page loads. Open dev tools in your browser and check the network tab, there you'll see that they pull the data from:
https://www.futbin.com/22/getPlayerSales?resourceId=239085&platform=pc
That returns a list of all the prices with dates in json.

Related

I am not able to get the response content from API call

I am doing a post request and reading the response as given
var response = client.PostAsync(uriBuilder.Uri.ToString(), Content).Result;
var value = response.Content.ReadAsStreamAsync().Result;
if the result is a proper json string, it populates that in 'value' variable
otherwise it returns empty.
but if run the same in postman or fiddler i get this as response
Is there a way to get this response too?
ReadAsStreamAsync() returns a Task<System.IO.Stream> and not a Task<string>. The json string that you see is probably the debugger showing you the content of the stream.
Consider using ReadAsStringAsync() to get the HTML content :
var response = await client.PostAsync(uriBuilder.Uri.ToString(), Content);
var result = await response.Content.ReadAsStringAsync();

Using http client to send header and content via post

I'm making a really simple call to an API to receive some data. I need to send headers to get authorized also I need to send some content on the body. This is what I came up with :
public async Task<List<LoremIpsum>> LoremIpsumJson()
{
LoremIpsum1 data = null;
try
{
var client = new HttpClient();
//now lets add headers . 1.method, 2.token
client.DefaultRequestHeaders.Add("Method", "LoremIpsumExample");
client.DefaultRequestHeaders.Add("Token", "sometoken");
HttpContent content = new StringContent("{\"Name\":\"John\",\"Surname\":\"Doe\",\"Example\":\"SomeNumber\"}", Encoding.UTF8, "application/json");
// ==edit==
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsync("www.theUrlToTheApi", content);
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
data = JsonConvert.DeserializeObject<QueueInfo>(json);
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message.ToString());
}
return data.data;
Debug.WriteLine(data.data);
}
The app breaks after response.EnsureSuccessStatusCode(); because the request obviously is not successful.
I think I'm really missing something really simple here. How can I do this call?
The error is
StatusCode: 406, ReasonPhrase: 'Not Acceptable'
There could be many reasons for this not working. For instance: keyvalues.ToString() is most likely not putting in the value you want. Seems like you might need to serialize to json rather than just calling .ToString().
Use a tool like postman first and get it working there so you have a working example then try and recreate in C#. It will make your life a lot easier.
For everyone coming here to find a solution.
HttpContent cannot take a header much o less a content-type. There was a typo on adding the content-type which was supposed to be added in HttpClient in this way:
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

How to send POST data through HttpClient and get the result of it after sending it?

I'm trying to obtain a published data from a ASP.NET file who publish all the data related to certain string i send in POST format.
The web-service is working fine and i already do the necesary test to see if is working properly.
Is valid mention than before the web-server was publishing all the data just fine, but i now want to add this aditional filter layer, to optimize the readings of the android app when reading the publication.
Here is the code of the method i use to get the data (who used to work just fine) but now i'm also using it to send a string to obtain certain specific data, and doesn't seems to work:
namespace AndroidApp.REST
{
public static class Client
{
public async static Task<T> GetRequest<T>(this string url)
{
try
{
HttpClient client = new HttpClient();
//Preparing to have something to read
var stringContent = new StringContent("someHardCodedStringToTellTheServerToPublishTheDataTheAppWillConsume");
var sending = await client.PostAsync(url, stringContent);
//Reading data
var response = await client.GetAsync(url);
var json = await response.Content.ReadAsStringAsync();
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
}
catch
{
return default(T);
}
}
}
}
Any question, comment, request for clarification or anything than helps to obtain an answer or improve the clarity of the question would be much apreciated too
Thanks in advance

How to clear json api response from windows phone 8.1

I am developig a windows app 8.1. Where I am calling an json api in a page say "Page1". My issue is that when I navigate to another page say "Page2" and come back to "Page1", I recive the same data from the api. I found that when I come back to the Page1 again, my web api is called but the result is come from I guess from cache.So, json data is not updated. How can I overcome with this issue. Any suggestion is most welcome.
My Rest API Call Code Is
var client = new HttpClient(); // Add: using System.Net.Http;
var response = await client.GetAsync(new Uri(url));
response.Headers.Add("Cache-Control", "no-cache");
var result = await response.Content.ReadAsStringAsync();
response.Dispose();
client.Dispose();
return ressult.
In the calling url just add a random parameters like
string uri = string.format(http://www.myservice.com?time={0}",DateTime.Now);
var response = await client.GetAsync(new Uri(uri));

How to send log in credentials and receive a html page in C# using a Windows Phone 8/8.1 app?

I am trying to access a html page to scrape data from my Windows Phone 8. But first i need to send log-in credentials to it. I am currently using htmlAgilityPack to scrape the data from the login page. I have tried to use WebBrowser instance to run in the background, but the html page is not automatically clickable and i cannot proceed. After which i tried to use the HttpClient class to send data using PostAsync() method, which only recieves a HttpResponseMessage, with which i have no clue what to do, but i am successfully able to send the credential data using a HttpContent object.
var httpResponseMessage = await client1.PostAsync("https://www.webpage.com", content);
After that i have been massively unsuccessful in progressing forward.
Thank you in advance for any help.
Here's how I use HttpClient class to communicate with a webservice in my project:
public async Task<string> httpPOST(string url, FormUrlEncodedContent content)
{
var httpClient = new HttpClient(new HttpClientHandler());
string resp = "";
HttpResponseMessage response = new HttpResponseMessage();
response = await httpClient.PostAsync(url, content);
try
{
response.EnsureSuccessStatusCode();
Task<string> getStringAsync = response.Content.ReadAsStringAsync();
resp = await getStringAsync;
}
catch (HttpRequestException)
{
resp = "NO_INTERNET";
}
return resp;
}
Here you can see how to retrieve the data. Hope it helps :)

Categories