-2
  $.post(loginurl, {username: $username, password: $password}, function(response) {

            if (response.session_id === null) {
                // alert("Please enter correct credentials!");
                return;
            }

Have written this but doesn't get me any result:

   //POST


      WebClient clt = new WebClient();
            var JsonLoginString = JsonConvert.SerializeObject(credentials);

            clt.Headers["Content-Type"] = "application/json";


            string check = "username:"+username+"password:"+password;


            clt.UploadStringAsync(new Uri(Config.loginurl), check);
            clt.UploadStringCompleted += clt_UploadStringCompleted;
  private void clt_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {

        }

EDIT:

I tried using this:

  public void Login(string username, string password)
    {
        var request = (HttpWebRequest)WebRequest.Create(Config.loginurl);
        request.ContentType = "application/json";
        request.Method = "POST";
        request.BeginGetRequestStream(new AsyncCallback(GetRequestCallBack), request);



    }

    private void GetRequestCallBack(IAsyncResult ar)
    {
        string username = "admin@something.com";
        string password = "admin";
        HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
        Stream postStream = request.EndGetRequestStream(ar);
        Byte[] byteData = Encoding.UTF8.GetBytes("username:"+username+"password:"+password);
        postStream.Write(byteData, 0, byteData.Length);
        postStream.Close();

        request.BeginGetResponse(new AsyncCallback(GetResponseCallBack), request);

    }
    string recMessage = "";
    private void GetResponseCallBack(IAsyncResult ar)
    {
        HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
        using (var response = request.EndGetResponse(ar))
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                SendData();
            });
            recMessage = reader.ReadToEnd();
        }

    }

    private void SendData()
    {
        MessageBox.Show(recMessage.ToString());
    }
vini
  • 4,657
  • 24
  • 82
  • 170

1 Answers1

1

HttpClient can help you. This is the Nuget package

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://www.url.com/");
    client.Content = new StringContent(jsonEncodedCredentials, Encoding.UTF8, "application/json"))

    HttpResponseMessage response = await client.PostAsync("api/login");
    if (response.IsSuccessStatusCode)
    {
        string jsonEncodedReponse = await response.Content.ReadAsStringAsync();
        //Do something with the response

    }
}
D.Rosado
  • 5,634
  • 3
  • 36
  • 56
  • what do i pass in the BaseAddress ? and i have edited my question – vini Jul 03 '14 at 10:16
  • if your login address is like 'http://www.mydomain.com/api/login', the BaseAddress is "http://www.mydomain.com/" and "api/login" is the resource – D.Rosado Jul 03 '14 at 11:25