S
S
stuckie2015-11-12 23:10:47
Microsoft Silverlight
stuckie, 2015-11-12 23:10:47

How to send json post requests to server on .Net Framework Silverlight?

I need to create an application for Windows Phone 7. For this I used the Windows Phone 7 SDK with Visual Studio 2010 in it.
In this application, I need to send json post requests to the server. For this case, I used this guide
https://code.msdn.microsoft.com/windowsapps/Window...
For I did not have standard methods.
I decided to use both methods with webclient and httpwebrequest. To begin with, I needed to send data entered by the user for authorization and receive in the form of user data and his key to perform other requests.
The server demanded data in the form: "Method, user key, parameters", and answered in the form of a request status ("OK" or "Error") and other parameters.
To work with Json, I used a third-party library from Newtonsoft.
As a result, I got this.

public static class Method
    {
        static object root = null;
        static string Login = null;
        static string Pass = null;
        
        private static void Wbc_UploadCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Result))
            {
                try
                {
                    root = JsonConvert.DeserializeObject(e.Result);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
        public static AuthorizedAnswer Authorizewbc(string login, string pass)
        {
            Uri URI = new Uri("http://bistrodrive.azurewebsites.net/api`");
            Request req = new Request();
            req.Method = "login";
            req.Parameters = new Dictionary<string, string>();
            req.Parameters.Add("login", login);
            req.Parameters.Add("password", pass);
            string myParameters = JsonConvert.SerializeObject(req);

            WebClient wc = new WebClient();
            //wc.Encoding = Encoding.UTF8;
            wc.Headers["Content-Type"] = "application/json";
            wc.Headers["Accept"] = "application/json";
            wc.Headers["X-Accept"] = "application/json";
            wc.UploadStringCompleted += Wbc_UploadCompleted;
            wc.UploadStringAsync(URI, "POST", myParameters);
            while (root == null) {    }
            return root as AuthorizedAnswer;
            
        }
        public static AuthorizedAnswer Authorizewbr(string login, string pass)
        {
            Login = login;
            Pass = pass;
            string AuthServiceUri = "http://bistrodrive.azurewebsites.net/api";
            HttpWebRequest spAuthReq = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest;
            spAuthReq.ContentType = "application/json";
            spAuthReq.Method = "POST";
            spAuthReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), spAuthReq);
            spAuthReq.Headers["X-Accept"] = "application/json";
            while (root == null) { }
            return root as AuthorizedAnswer;
        }
        static void GetRequestStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            Request req = new Request();
            Stream postStream = myRequest.EndGetRequestStream(callbackResult);
            req.Method = "login";
            req.Parameters = new Dictionary<string, string>();
            req.Parameters.Add("login", Login);
            req.Parameters.Add("password", Pass);
            string postData = JsonConvert.SerializeObject(req);
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();
            myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
        }
        static void GetResponsetStreamCallback(IAsyncResult callbackResult)
        {

            try
            {
                HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
                string responseString = "";
                Stream streamResponse = response.GetResponseStream();
                StreamReader reader = new StreamReader(streamResponse);
                responseString = reader.ReadToEnd();
                streamResponse.Close();
                reader.Close();
                response.Close();
                string result = responseString;
                root = JsonConvert.DeserializeObject(result);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
    }
public class AuthorizedAnswer
    {
        public String Status { get; set; }
        public List<String> Result { get; set; }
    }
public class Request
    {    
        public string Method { set; get; }
        public Dictionary<string,string> Parameters { set; get; }
        public string Token { set; get; }
    }
}

As a result, the server simply did not send a response. Why - I have no idea.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question