2017-12-25 6 views
1

Nun ... ich eine Menge Fragen hier in Stackoverflow zu lesen, aber Antwort für sie immer noch nicht bekommen, habe ich diesen Web-API-Controller:Httpclient sendet null POST-Daten

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; 
    } 

} 

und ich schrieb eine kleine Tester es:

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(); 
} 

ich bekomme immer NULL auf den Parameter Data in der API.

Ich habe versucht, diese Zeilen an den Tester hinzufügen:

var values = new Dictionary<string, string>(); 
values.Add("Data", "Data"); 
var content = new FormUrlEncodedContent(values); 

noch NULL:

client.DefaultRequestHeaders 
          .Accept 
          .Add(new MediaTypeWithQualityHeaderValue("application/json")); 

noch NULL, ich den Inhalt mit auch ersetzen.

Ich habe versucht, die Anforderung an Schalt: '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); 
}); 

aber Debugger noch sagen Die Data ist immer noch NULL.

Ich bekomme die ID ohne Probleme.

+1

auf Nebenbei bemerkt, ' Hallo' xml mir scheint, nicht json als Inhaltstyp schlägt –

Antwort

1

Wenn Sie es als JSON-String senden möchten, sollten Sie dies tun (mit Newtonsoft.Json):

var serialized = JsonConvert.SerializeObject("Hello"); 
var content = new StringContent(serialized, Encoding.UTF8, "application/json"); 

Du hast es fast direkt mit FormUrlEncodedContent, was man tun musste, war es mit einem leeren schicken Name, wie in this example:

var content = new FormUrlEncodedContent(new[] 
{ 
    new KeyValuePair<string, string>("", "Hello") 
}); 

var response = client.PostAsync("api/ERS?ID=123", content); 
+0

thanks man! das hat gut funktioniert ... verstehe aber nicht warum – Izikon