简体   繁体   中英

HTTPClient post not working windows phone silverlight

I'm making a silverlight application. Now I have this function for doing a POST

  public async Task<Webservice> addEvent()
        {
                 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();

        }

But I gpt build errors on FormUrlEncodedContent can anyone help?

This is the error:

The type or namespace name 'FormUrlEncodedContent' could not be found (are you missing a using directive or an assembly reference?)

I was having the same problem, and what I ended up having to do was install from NuGet the Microsoft.Net.Http package.

Right click project name in solution explorer.
Manage NuGet Packages.
Search for "microsoft.net.http".
Install.

string site = "https://www.yoursite.com";
var pairs = 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 client = new System.Net.Http.HttpClient();
var content = new FormUrlEncodedContent(pairs);

System.Net.Http.HttpResponseMessage response = await client.PostAsync(site, content);

if (response.IsSuccessStatusCode)
{

}

(tested on windows phone 8.1 and 10)

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