user3821206 February 2016

send login data with POST method in a universal app

I am working a windows app store,and I am trying to send login and password values,this is my 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 like this:

'WebRequest' does not contain a definition for 'Content Length' and no extension method 'ContentLength' accepting a first argument of type 'WebRequest' was found (a using directive or an assembly reference is she missing ?)
'WebRequest' does not contain a definition for 'GetRequestStream' and no extension method 'GetRequestStream' accepting a first argument of type 'WebRequest' was found (a using directive or an assembly reference is she missing? )
'WebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'WebRequest' was found (a using directive or an assembly reference is she missing? )

I am new in universal windows Apps,so have you please any idea how can I correct my code to send login data to the server thanks for help

Answers


Mehrzad Chehraz February 2016

As you have noticed, WebRequest class in .NET Core is a little bit different than traditional .NET. First of all, I suggest you to look at the HttpClient class which is the default class in UWP for working with HTTP.

If you want to go with the WebRequest, in order to set Content-Length header, use the Headers property:

request.Headers["ContentLength"] = length;

In order to get request and response streams, you need to use the async method, GetRequestStreamAsync and GetResponseAsync. Synchronous methods are not available.

Your code will look like this 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)) {
         //
     }
   }
}

Post Status

Asked in February 2016
Viewed 2,193 times
Voted 7
Answered 1 times

Search




Leave an answer