C# and Daniweb's API: Example, nasty, simple and quick code
Mike Askew commented: Thanks for sharing, +rep +6
private void button1_Click(object sender, EventArgs e)
{
string newString = "";
var client = new RestClient("http://www.daniweb.com/api"); /*I prepare the API*/
/*I remove whitespaces except actual spaces*/
for (int i = 0; i < textBox1.Text.Length; i++)
{
if ((char.IsWhiteSpace(textBox1.Text[i])) && (!textBox1.Text[i].Equals(' ')))
{
textBox1.Text = textBox1.Text.Remove(i, 1);
}
}
/*I make sure that the user name isnt empty */
if (textBox1.TextLength==0)
{
MessageBox.Show("Please insert a valid member");
}
else
{
/*I call the members method of the API and pass parameters by GET. POST did not work (Why?)*/
var request = new RestRequest("members", Method.GET);
request.AddParameter("username", textBox1.Text);
/*Send it and wait for reply*/
var response = client.Execute(request);
/*Finding where it says the post number in the response...*/
int next = 0;
int first = response.Content.IndexOf("\"posts\":\"");
if (first==-1) /*-1 means it could not find it therefore no member exists*/
{
MessageBox.Show("Member not found!");
}
else
{
/*I find the next quote that actually begins the post number*/
do
{
next = response.Content.IndexOf("\"", first);
/*Not neccesary but in case Dani changes the array to something else....*/
if (next==-1)
{
break;
}
} while ((next==0));
/*VERY Ugly hack to get posts numbers. Tried with largest post numbers I could find (Dani's AFAIK)*/
string str2 = response.Content.Substring(first, (next + 16) - first);
newString = Regex.Replace(str2, "[^.0-9]", "");
}
if (textBox1.TextLength == 0)
{
MessageBox.Show("Please insert a valid member");
}
else if (first!=-1)
{
MessageBox.Show(newString, "The total amount of posts this user has is:");
}
}
}
ddanbe 2,724 Professional Procrastinator Featured Poster
Ketsuekiame 860 Master Poster Featured Poster
Ketsuekiame 860 Master Poster Featured Poster
riahc3 50 Â Team Colleague
riahc3 50 Â Team Colleague
Ketsuekiame 860 Master Poster Featured Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
tux4life commented: You are modest. This is for all the other times you've helped as well. ;) +0
riahc3 50 Â Team Colleague
Ketsuekiame 860 Master Poster Featured Poster
Ketsuekiame 860 Master Poster Featured Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
ddanbe commented: Very well spoken, like your signare. +14
riahc3 50 Â Team Colleague
kplcjl 17 Junior Poster
kplcjl 17 Junior Poster
riahc3 50 Â Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.