c# - Cannot find certain words in context, missing reference, brain about to explode? -
i'm trying learn c#/silverlight/windows phone 7. what's going on here: when try use examples straight off of ms's msdn site, , kinds of errors:
for example:
using system; using system.io; using system.collections.generic; using system.device.location; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using microsoft.phone.controls; using microsoft.phone.tasks; using system.device.location; using microsoft.phone.reactive; private void registerphone(object sender, routedeventargs e) { // create request using url can receive post. webrequest request = webrequest.create("http://www.contoso.com/postaccepter.aspx "); // set method property of request post. request.method = "post"; // create post data , convert byte array. string postdata = "this test posts string web server."; byte[] bytearray = encoding.utf8.getbytes(postdata); // set contenttype property of webrequest. request.contenttype = "application/x-www-form-urlencoded"; // set contentlength property of webrequest. request.contentlength = bytearray.length; // request stream. stream datastream = request.getrequeststream(); // write data request stream. datastream.write(bytearray, 0, bytearray.length); // close stream object. datastream.close(); // response. webresponse response = request.getresponse(); // display status. console.writeline(((httpwebresponse)response).statusdescription); // stream containing content returned server. datastream = response.getresponsestream(); // open stream using streamreader easy access. streamreader reader = new streamreader(datastream); // read content. string responsefromserver = reader.readtoend(); // display content. console.writeline(responsefromserver); // clean streams. reader.close(); datastream.close(); response.close(); }
it tells me
1) the name 'encoding' not exist in context
2) system.net.webrequest not contain definition getstreamrequest , no extension method getrequeststream accepting first argument of type system.net.webrequest found (are missing using directive... etc
3) similar message contentlength
4) similar message getresponse
i have no idea libraries need "using" , when think i'm "using" right ones, gives me errors. doing wrong?
the encoding
class resides in system.text
namespace. have add "usings".
as far other messages, has limitations of silverlight. msdn samples, presume, taken "big" version of .net framework, weren't they?
in silverlight, see, things not allowed in order prevent undesirable application behavior (such blocking calls), , things not supported due resources limitation.
in particular, contentlength
1 of latter. library determine content length based on actual amount of data write request. remove line, you'll ok.
but getresponsestream()
1 of former. has fact operation implies opening network connection, may take while. , since blocking calls not allowed in silverlight, getresponsestream()
method has go.
instead, you're supposed use so-called "asynchronous pattern" - is, pair of methods begingetresponsestream
/endgetresponsestream
. calling begin
method , providing callback called once operation complete. then, inside callback, use end
method result of operation. so:
request.begingetrequeststream( ar => { var datastream = request.endgetrequeststream( ar ); // write data request stream. datastream.write( bytearray, 0, bytearray.length ); // close stream object. datastream.close(); // , on... }, null );
same goes getresponse
method.
Comments
Post a Comment