我目前正在开发一个wp8.1应用程序C#,我已经设法通过从textbox.texts创建一个json对象(bm)在json中执行一个POST方法到我的api。 这是我的代码如下。我如何使用相同的textbox.text并将它们作为内容类型= application / x-www-form-urlencoded进行POST。该代码是什么?
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;
答案 0 :(得分:98)
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);
或者
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);
答案 1 :(得分:7)
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
答案 2 :(得分:6)
对我来说最好的解决方案是:
...
import org.springframework.http.ResponseEntity;
...
public ResponseEntity<List<Dto>> search() {
DtoClient client = DtoClient.connect();
return client.search();
}
答案 3 :(得分:2)
发布此内容类型且不使用字典的另一种变体是:
StringContent postData = new StringContent(JSON_CONTENT, Encoding.UTF8, "application/x-www-form-urlencoded");
using (HttpResponseMessage result = httpClient.PostAsync(url, postData).Result)
{
string resultJson = result.Content.ReadAsStringAsync().Result;
}
答案 4 :(得分:1)
我正在使用具有[FromBody]
属性的.Net Core 2.1 API,并且必须使用以下解决方案才能成功将其发布:
_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"));
答案 5 :(得分:0)
您可以这样设置值,并将其发送到PostAsync
方法:
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);