Search code examples
c#windows-phone-8webrequestwebresponse

Windows Phone 8.1 webrequest POST get response


I'm trying to make a web request with POST data and get back the response. I've got it working for Android, but it doesn't work the same way for Windows Phone and I can't seem to get it to work. I'm currently getting errors for GetRequestStreamd and GetResponse. I've tried it with async and await but it returned null pointers. I used the following code in Android:

public static string Login(string user, string pwd)
    {
        WebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("myurl");
        httpWebRequest.Method = "POST";
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            LoginData data = new LoginData
            {
                userid = user,
                password = pwd
            };
            var json = JsonConvert.SerializeObject(data);

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Dispose();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            return streamReader.ReadToEnd();
        }
    }

Solution

  • I've got it working! Answer is shown in code below.

    public static string postData;
        public static string responseString;
    
        public static async Task PostJsonRequest()
        {
            string AuthServiceUri = "myurl"; 
            HttpWebRequest spAuthReq = WebRequest.Create(AuthServiceUri) as HttpWebRequest;
            spAuthReq.ContentType = "application/json";
            spAuthReq.Method = "POST";
            spAuthReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), spAuthReq);
        }
    
        public static void GetRequestStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            Stream postStream = myRequest.EndGetRequestStream(callbackResult);
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Dispose();
            myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
        }
    
        public static void GetResponsetStreamCallback(IAsyncResult callbackResult)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
    
                Stream streamResponse = response.GetResponseStream();
                StreamReader reader = new StreamReader(streamResponse);
                responseString = reader.ReadToEnd();
                streamResponse.Dispose();
                reader.Dispose();
                response.Dispose();
            }
            catch (Exception e)
            {
    
            }
        }
    
        public static void EnterLoginValues(string user, string pwd)
        {
            LoginData data = new LoginData
            {
                userid = user,
                password = pwd
            };
            postData = JsonConvert.SerializeObject(data);
        }