음 ... I에 유래 여기에 질문을 많이 읽을 수 있지만 여전히에 대한 답을하지 않았다, 나는이 웹 API 컨트롤러가 있습니다HttpClient를 보내 널 POST 데이터
public class ERSController : ApiController
{
[HttpGet]
public HttpResponseMessage Get()
{
var resposne = new HttpResponseMessage(HttpStatusCode.OK);
resposne.Content = new StringContent("test OK");
return resposne;
}
[HttpPost]
public HttpResponseMessage Post([FromUri]string ID,[FromBody] string Data)
{
var resposne = new HttpResponseMessage(HttpStatusCode.OK);
//Some actions with database
resposne.Content = new StringContent("Added");
return resposne;
}
}
을하고 내가 쓴 그것은 작은 테스터 :
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:54916/");
client.DefaultRequestHeaders.Accept.Clear();
var content = new StringContent("<data>Hello</data>", Encoding.UTF8, "application/json");
var response = client.PostAsync("api/ERS?ID=123", content);
response.ContinueWith(p =>
{
string result = p.Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
});
Console.ReadKey();
}
난 항상 API의 매개 변수 Data
에 NULL
를 얻을.
나는 테스터에 그 라인을 추가하는 시도 : 아직
var values = new Dictionary<string, string>();
values.Add("Data", "Data");
var content = new FormUrlEncodedContent(values);
NULL
:
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
여전히
NULL
, 나는 또한 함께 컨텐츠를 교체합니다.
나는에 요청을 전환 시도 '! NO'
WebClient client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
var values = new NameValueCollection();
values["Data"] = "hello";
var task = client.UploadValuesTaskAsync("http://localhost:54916/api/ERS?ID=123", values);
task.ContinueWith((p) =>
{
string response = Encoding.UTF8.GetString(p.Result);
Console.WriteLine(response);
});
하지만 디버거가 여전히 말 Data
은 여전히 NULL
입니다.
나는 문제없이 ID를 얻습니다.
가'나 XML을 보인다 콘텐츠 유형으로하지 JSON은 –