How to clear json api response from windows phone 8.1 - c#

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));

Related

glitch.com project can't be called with c#

I am writing this question in context of glitch.com projects.
so, i made a test project(asp.net .net6 webapi) in glitch.com which returns your ip address. The link to the webpage is https://ror-test.glitch.me/getip . It runs perfectly fine and returns response accurately when called from a browser. Now, I want to access this project from c# client (to be precise unity3d) however i am unable to access it.
My first try was to use httpclient.getstringasync-
Code-
HttpClient client = new HttpClient();
string response = await client.GetStringAsync(url);
System.Console.WriteLine(response);
Output-
Unhandled exception. System.Net.Http.HttpRequestException: Response status code does not indicate success: 403 (Forbidden).
In my second try i used httpclient.getasync
Code-
HttpClient client = new HttpClient();
var response = await client.GetAsync(url);
Output-
Response - https://jpst.it/348Ik
Response.Content - https://jpst.it/348LS
Also just to say that my app is working perfectly fine when i call the project from nodejs it works perfectly-
Code -
var url = "https://ror-test.glitch.me/getip";
var XMLHttpRequest = require("xhr2");
var xhr = new XMLHttpRequest();
console.log("starting");
xhr.open("GET", url);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
}};
xhr.send();
Output -
starting
200
your ip: xx.xx.xxx.xx
In place of xx.xx.xxx.xx my real ip which i have cross checked with https://whatismyipaddress.com/ is coming. so i am sure problem is with c# client.
Plz help me or any other way i can call it from c# client(precisely unity3d).
You need to pass User-Agent request header.
HttpClient client = new HttpClient();
//add user agent
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "fake");
var response = await client.GetAsync(url);

HttpClient c#- how to get dynamic content data?

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.

How to consume web api from Asp.net web api application

I have my own Web API application in which i want to call the another api which is on server. How can i do that ?
var result = url(http://54.193.102.251/CBR/api/User?EmpID=1&ClientID=4&Status=true);
// Something like this.
You can use HttpClient. Here is a sample of async method which calls your API:
var client = new HttpClient();
client.BaseAddress = new Uri("http://54.193.102.251/CBR/api");
// here you can add additional information like authorization or accept headers
client.DefaultRequestHeaders
.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// you can chose which http method to use
var response = await client.GetAsync("User?EmpID=1&ClientID=4&Status=true");
if (!response.IsSuccessStatusCode)
return; // process error response here
var json = await response.Content.ReadAsStringAsync(); // assume your API supports json
// deserialize json here

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 :)

How to consume a WCF Service in Windows Phone 8.1

I am trying to call a WCF service from a windows phone 8.1 app, the option to add a service reference no longer exists.
I tried this:
HttpClient httpClient = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://serverURl/serviceName.svc/methodName?variableName=value");
HttpResponseMessage response = await httpClient.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();
But it didnt work, the string is always empty.
N.B The server is also not local host so im not facing the problem of connecting from windows phone emulator to local host.
Try this.
If its not working, let me know.
HttpClient httpClient = new System.Net.Http.HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,"http://localhost:18362/Service1.svc/GetData");
HttpResponseMessage response = await httpClient.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();
var dialog = new MessageDialog(data);
await dialog.ShowAsync();
Windows Phone Store apps in Windows Phone 8.1 do not support the System.ServiceModel namespace.
There is a workaround you can
Read here.
You can use System.Net.Http :
public async Task<string> GetMethod(string url)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(url); // The Get Process to get result from WCF service
string result = await response.Content.ReadAsStringAsync(); // Get Json string result
return result;
}
}

Categories