简体   繁体   中英

Post Method in Windows Phone 8

I need to send some parameters to a php server using post method in WP8 app and get response in json format. I've tried everything that I found on stackoverflow and other sites, still could not.

The last code piece I come up with is:

public static async void GetData(string url, string data)
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.PostAsync(new Uri(url), new StringContent(data));

    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    MessageBox.Show(responseBody); // just showing the response for now
}

It shows a message from the server (which is an error stating some fields are missing) that it actually communicates with the server but the problem is with sending data. I call the above method like:

GetData("http_address_here", "?action=REGISTER&email=asc@fsdf.com&password=54561wefwe&firstname=sdfsdf&lastname=sdfsdf&picture=10");

But I saw an example sending data in xml. Possibly the mistake is about calling the method. Having seen tens of sample codes and trying everything, I really got confused about it. Any help is appreciated.

I finally managed to solve the problem. Thanks everyone for helping. The working code is below, it uses POST method and the resulting json object is stored as a string.

var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("email", "qewfwef"),
                    new KeyValuePair<string, string>("password", "qewfwef"),
                    new KeyValuePair<string, string>("firstname", "qewfwef"),
                    new KeyValuePair<string, string>("lastname", "qewfwef"),
                    new KeyValuePair<string, string>("picture", "123456")
                };

        var httpClient = new HttpClient(new HttpClientHandler());
        HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(values));
        response.EnsureSuccessStatusCode();
        var responseString = await response.Content.ReadAsStringAsync();

        MessageBox.Show(responseString.ToString()); // just to see what we get

Can you try this

public static async Task<string> SendRequestPostResponse()
    {
        try
        {
            var postRequest = (HttpWebRequest)WebRequest.Create("your Url Here");

            postRequest.ContentType = "application/x-www-form-urlencoded"; // Whichever content type you want to POST
            postRequest.Method = "POST";

            using (var requestStream = await postRequest.GetRequestStreamAsync())
            {
                byte[] postDataArray = Encoding.UTF8.GetBytes("your data here"); // Data for the POST request here
                await requestStream.WriteAsync(postDataArray, 0, postDataArray.Length);
            }

            WebResponse postResponse = await postRequest.GetResponseAsync();

            if (postResponse != null)
            {
                var postResponseStream = postResponse.GetResponseStream();
                var postStreamReader = new StreamReader(postResponseStream);

                string response = await postStreamReader.ReadToEndAsync();

                return response;
            }
            return null;
        }
}

HTTPWebRequest将是另一种选择。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM