Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

HttpWebRequest / HttpClient wont post

$
0
0

` public async Task ValidateUser(string username, string password)
{
//Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var m = new UserModel
        {
            user = new List<user>
            {
                new user{username = username, password=password}
            }

        };



        var Sercontent = JsonConvert.SerializeObject(m);
        //var content = new StringContent(Sercontent, Encoding.UTF8, "application/json");

        //var response = await Client.PostAsync(URL, content).ConfigureAwait(false); //breaks here
        //if (response.IsSuccessStatusCode)
        //    return await response.Content.ReadAsStringAsync();
        //return null;

        var http = (HttpWebRequest)WebRequest.Create(new Uri(URL2));
        http.Accept = "application/json";
        http.ContentType = "application/json";
        http.Method = "POST";

        UTF8Encoding enc = new UTF8Encoding();
        Byte[] bytes = enc.GetBytes(Sercontent);

        using (var stream = await Task.Factory.FromAsync<Stream>(http.BeginGetRequestStream, http.EndGetRequestStream, null)) //breaks here
        {
            stream.Write(bytes, 0, bytes.Length);
            stream.Dispose();
        }


        using (var response = (HttpWebResponse)await Task.Factory.FromAsync<WebResponse>(http.BeginGetResponse‌​, http.EndGetResponse, null))
        {
            var responseStream = response.GetResponseStream();
            var sr = new StreamReader(responseStream);
            var content = sr.ReadToEnd();
            return content;
        }

}`

The app gets unhandled exception, idk why, I have tried this using console app and it works magically, only using xamarin forms makes it hard.
I am also using .svc as web service but yea we treat it as http web.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>