반응형
HTTPclient 콘텐츠 유형 = application / x-www-form-urlencoded를 사용하여 POST하는 방법
현재 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;
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);
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
[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"));
다음과 같이 값을 설정하고 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);
반응형
'program story' 카테고리의 다른 글
오류 : ggplot2 및 data.table에 대한 패키지 또는 네임 스페이스로드 실패 (0) | 2020.11.11 |
---|---|
TensorFlow : InternalError : Blas SGEMM 시작 실패 (0) | 2020.11.11 |
REST API에서 JSON을 반환하는 경우 어떤 MIME 유형입니까? (0) | 2020.11.11 |
Java에서 길이를 알 수없는 바이트 배열 (0) | 2020.11.11 |
간단한 '평균'함수를 좌절시키는 Haskell 유형 (0) | 2020.11.11 |