我目前正在开发wp8.1应用程序C#,通过从textbox.texts创建json对象(bm),我设法在json中对我的api执行POST方法。这是我的下面的代码。我该如何使用相同的textbox.text并将其发布为内容类型= application / x-www-form-urlencoded。这是什么代码?
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;
FormUrlEncodedContent
,因为字典是IEnumerable
的KeyValuePair
秒。