c# - send login data with POST method in a universal app -


i working windows app store,and trying send login , password values,this code:

 try         {             string user = login.text;             string pass = password.password;              asciiencoding encoding = new asciiencoding();             string postdata = "login=" + user + "&mdp=" + pass;             byte[] data = encoding.getbytes(postdata);              webrequest request = webrequest.create("myurl/login.php");             request.method = "post";              request.contenttype = "application/x-www-form-urlencoded";             request.contentlength = data.length;              stream stream = request.getrequeststream();             stream.write(data, 0, data.length);             stream.dispose();              webresponse response = request.getresponse();             stream = response.getresponsestream();              streamreader sr = new streamreader(stream);               sr.dispose();             stream.dispose();           }         catch (exception ex)         {             ex.message.tostring();         } 

i have many errors this:

'webrequest' not contain definition 'content length' , no extension method 'contentlength' accepting first argument of type 'webrequest' found (a using directive or assembly reference missing ?) 'webrequest' not contain definition 'getrequeststream' , no extension method 'getrequeststream' accepting first argument of type 'webrequest' found (a using directive or assembly reference missing? ) 'webrequest' not contain definition 'getresponse' , no extension method 'getresponse' accepting first argument of type 'webrequest' found (a using directive or assembly reference missing? ) 

i new in universal windows apps,so have please idea how can correct code send login data server help

as have noticed, webrequest class in .net core little bit different traditional .net. first of all, suggest @ httpclient class default class in uwp working http.

if want go webrequest, in order set content-length header, use headers property:

request.headers["contentlength"] = length; 

in order request , response streams, need use async method, getrequeststreamasync , getresponseasync. synchronous methods not available.

your code after all:

string user = login.text; string user = login.text; string pass = password.password;  asciiencoding encoding = new asciiencoding(); string postdata = "login=" + user + "&mdp=" + pass; byte[] data = encoding.getbytes(postdata);  webrequest request = webrequest.create("myurl/login.php"); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; request.headers["contentlength"] = data.length.tostring();  using (stream stream = await request.getrequeststreamasync()) {    stream.write(data, 0, data.length); }  using (webresponse response = await request.getresponseasync()) {    using (stream stream = response.getresponsestream()) {       using (streamreader sr = new streamreader(stream)) {          //      }    } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -