I made a custom website using txti.es which only has a "Hello World" text in it here. Then I wrote this simple code which extract the text from my website from line 20
public static string GetValueFromOnlineSource(string urlAddress) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string tempString = "";
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}
string line;
int count = 0;
while ((line = readStream.ReadLine()) != null) // The Data in line 20
{
count++; if (count == 20) { TheInfoLine = line; }
}
int CountLetters = 0;
foreach (var item in TheInfoLine)
{
if (CountLetters > 2 && CountLetters < TheInfoLine.Length - 4) // Remove Unwanted Chars from line 20
{
tempString += item;
}
CountLetters++;
}
response.Close();
readStream.Close();
}
return tempString;
}
now Calling this method like this :
Conole.WriteLine(GetValueFromOnlineSource("http://txti.es/testweb"));
Will output the text inside the website
Hello World
I'm considering using this to show, change or even remove some values from an application that has let say 10,000 users without the need of an update.
Now, I'm fairly new to any network related coding, and i'm not sure if what i'm doing is considered good, bad or reliable. What would you change ?
Aucun commentaire:
Enregistrer un commentaire