C
C#•3y ago
BlackBull

string query in C Sharp

i am trying to get values from 2 website links and assign them to a lable using visual studio one is saying how many players online http://blackbullstudio.ddns.net:81/Websiteassets/GTA/players.php the other says if the server is online http://blackbullstudio.ddns.net:81/Websiteassets/GTA/status.php so that in the end it will look like in the image
26 Replies
BlackBull
BlackBullOP•3y ago
im working in .cs so i think this is the right section the body of the pages look like this <body>Online</body> <body>0/8</body> it shows only the data i want to get i tried this private void Form1_Load(object sender, EventArgs e) { String queryString = "http://blackbullstudio.ddns.net:81/Websiteassets/GTA/players.php?"; }
becquerel
becquerel•3y ago
you need to get the label you want to change and set its Text property e.g.
private void Form1_Load(blah...)
{
myLabel.Text = "hello world";
}
private void Form1_Load(blah...)
{
myLabel.Text = "hello world";
}
BlackBull
BlackBullOP•3y ago
yeah i get that but the url i get needs to return the data i need
becquerel
becquerel•3y ago
if you right-click on the label and go to 'properties', i think it shows you the name there. been a while since i did winforms oh, you actually wanna call the page
BlackBull
BlackBullOP•3y ago
yeah i need the info thats on the page in the label Xd http://blackbullstudio.ddns.net:81/Websiteassets/GTA/players.php like that one its says 0/8 i need that text in the label cause it says how many players there are
becquerel
becquerel•3y ago
private async void method(blah)
{
// create thing that does downloads
var client = new HttpClient();

// download, get website content
var content = await client.GetAsync("www.yoururl.com");

// use downloaded content
yourLabel.Text = content;
}
private async void method(blah)
{
// create thing that does downloads
var client = new HttpClient();

// download, get website content
var content = await client.GetAsync("www.yoururl.com");

// use downloaded content
yourLabel.Text = content;
}
BlackBull
BlackBullOP•3y ago
it says this Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to 'string' FivemLauncher C:\Users\PC\source\repos\FivemLauncher\FivemLauncher\Form1.cs 24 Active
becquerel
becquerel•3y ago
yeah, i forgot the await keyword need that
BlackBull
BlackBullOP•3y ago
lots of red Xd
BlackBull
BlackBullOP•3y ago
becquerel
becquerel•3y ago
oh, i know what i'm forgetting
private async void method(blah)
{
// create thing that does downloads
var client = new HttpClient();

// download, get website content
var response = await client.GetAsync("www.yoururl.com");

// unwrap the HTTP response
var content = await response.Content.ReadAsStringAsync();

// use downloaded content
yourLabel.Text = content;
}
private async void method(blah)
{
// create thing that does downloads
var client = new HttpClient();

// download, get website content
var response = await client.GetAsync("www.yoururl.com");

// unwrap the HTTP response
var content = await response.Content.ReadAsStringAsync();

// use downloaded content
yourLabel.Text = content;
}
my bad
BlackBull
BlackBullOP•3y ago
no worries im allready happy that your helping me
becquerel
becquerel•3y ago
this isn't technically the best way to do things. but it's fine for getting started and no prob
BlackBull
BlackBullOP•3y ago
do i need some models like the useing system.net etc cause some are still red
BlackBull
BlackBullOP•3y ago
BlackBull
BlackBullOP•3y ago
you know if it works it works simple as that Xd
becquerel
becquerel•3y ago
you will yeah though i think the error there is different
BlackBull
BlackBullOP•3y ago
i have these rn using System.Net; using System.Text; using static System.Formats.Asn1.AsnWriter; using System.Xml.Linq; using System.Web;
becquerel
becquerel•3y ago
you need async in your method definition private async void
BlackBull
BlackBullOP•3y ago
aah okey
becquerel
becquerel•3y ago
async lets you use await, await lets you make HTTP calls without freezing your app, basically
BlackBull
BlackBullOP•3y ago
aah okey IT WORKS !! Thank you soo much i ahve been strugling with this for like 2 hours now haha i started using C# today haha
becquerel
becquerel•3y ago
🙂 glad to hear it everyone struggles at first, don't worry about it
BlackBull
BlackBullOP•3y ago
hehe yeah its difrent from what im used to Xd im mainly a python / web developer
becquerel
becquerel•3y ago
eventually it starts feeling natural lemme know if there's anything else i can help with
BlackBull
BlackBullOP•3y ago
true Xd i will thanks !solved

Did you find this page helpful?