wake-up-neo.com

Gewusst wie: POST using HTTPclient content type = application / x-www-form-urlencoded

Ich entwickle gerade eine wp8.1-Anwendung in C #. Ich habe es geschafft, eine POST -Methode in JSON auf meiner API auszuführen, indem ich ein JSON-Objekt (BM) aus textbox.texts erstellte. Hier ist mein Code Wie nehme ich das gleiche textbox.text und POST= als Inhaltstyp = application/x-www-form-urlencoded. Was ist der Code dafür?

            Profile bm = new Profile();
            bm.first_name = Names.Text;
            bm.surname = surname.Text;

            string json = JsonConvert.SerializeObject(bm);

            MessageDialog messageDialog = new MessageDialog(json);//Text should not be empty 
            await messageDialog.ShowAsync();

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

            byte[] messageBytes = Encoding.UTF8.GetBytes(json);
            var content = new ByteArrayContent(messageBytes);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = client.PostAsync("myapiurl", content).Result;
64
Prince Achim
var nvc = new List<KeyValuePair<string, string>>();
nvc.Add(new KeyValuePair<string, string>("Input1", "TEST2"));
nvc.Add(new KeyValuePair<string, string>("Input2", "TEST2"));
var client = new HttpClient();
var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(nvc) };
var res = await client.SendAsync(req);

Oder

var dict = new Dictionary<string, string>();
dict.Add("Input1", "TEST2");
dict.Add("Input2", "TEST2");
var client = new HttpClient();
var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(dict) };
var res = await client.SendAsync(req);
112
 var params= new Dictionary<string, string>();
 var url ="Please enter URLhere"; 
 params.Add("key1", "value1");
 params.Add("key2", "value2");

 using (HttpClient client = new HttpClient())
  {
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage response = client.PostAsync(url, new FormUrlEncodedContent(dict)).Result;
              var tokne= response.Content.ReadAsStringAsync().Result;
}

//Get response as expected
6
Bhushan Teli

Ich habe eine .Net Core 2.1-API mit dem [FromBody] Attribut und ich musste die folgende Lösung verwenden, um erfolgreich zu posten:

_apiClient =  new HttpClient();
_apiClient.BaseAddress = new Uri(<YOUR API>);
var MyObject myObject = new MyObject(){
    FirstName = "Me",
    LastName = "Myself"
};

var stringified = JsonConvert.SerializeObject(myObject);
var result = await _apiClient.PostAsync("api/appusers", new StringContent(stringified, Encoding.UTF8, "application/json"));
0
Helzgate

Sie können die Werte wie folgt festlegen und an die Methode PostAsync senden:

var apiClient = new HttpClient();
var values = new Dictionary<object, object>
{
    {"key1", val1},
    {"key2", "val2"}
};

var content = new StringContent(JsonConvert.SerializeObject(values), Encoding.UTF8, "application/json");
var response = await apiClient.PostAsync("YOUR_API_ADDRESS", content);
0
Pedram