c# - content from a website in a text file -
my aim content website (for instance league table sports website) , put in .txt file can code local file.
i have tried multiple lines of code , others examples such as:
// prepare web page asking httpwebrequest request = (httpwebrequest) webrequest.create("http://www.stackoverflow.com"); // prepare web page asking httpwebrequest request = (httpwebrequest) webrequest.create("http://www.stackoverflow.com"); // execute request httpwebresponse response = (httpwebresponse)request.getresponse(); // read data via response stream stream resstream = response.getresponsestream(); string tempstring = null; int count = 0; { // fill buffer data count = resstream.read(buf, 0, buf.length); // make sure read data if (count != 0) { // translate bytes ascii text tempstring = encoding.ascii.getstring(buf, 0, count); // continue building string sb.append(tempstring); } while (count > 0); // more data read? }
my issue when trying this, words request , response underlined in read , tokens invalid. there better method content website .txt file or there way fix code supplied?
thanks
is there way fix code supplied?
the code submitted works me, make sure have proper name spaces defined. in case : using system.net;
or might duplicate creation of variable request isn't typo? if remove 1 of request variables.
is there better method content website .txt file
since you're reading content site anyway there isn't need while loop. instead can use readtoend method supplied streamreader.
string sitecontent = ""; using (streamreader reader = new streamreader(resstream)) { sitecontent = reader.readtoend(); }
also sure dispose of webresponse, other code should work fine.
Comments
Post a Comment