2016-07-25 18 views
0

ich einen Web-api habe, dass ich erfolgreich über einen Browser zugreifen kann: -C# WebAPI POST über Konsolenprogramm

https://127.0.0.1:8443/ncrApi

Ich versuche, ein einfaches Konsolenprogramm in C# VS2015 zu erstellen, Daten zu senden und erhalten Sie eine Antwort mit http POST. Hier

ist das, was ich bisher: -

using System; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Threading.Tasks; 

namespace WebSample 
{ 

    class ApiSendData 
    { 
     public string UserID { get; set;} // username 
     public string Password { get; set;} // password for the webapi 

     public string ApiFunction { get; set; } 
     public string DppName { get; set; } 
     public string ClearData { get; set; } 
     public string DppVersion { get; set; } 
    } 



    class Program 
    { 
     static void Main(string[] args) 
     { 
      // The Main function calls an async method named RunAsync 
      // and then blocks until RunAsyncc completes. 
      RunAsync().Wait(); 
     } 

     static async Task RunAsync() 
     { 
      using (var client = new HttpClient()) 
      { 

       //specify to use TLS 1.2 as default connection 
       System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; 

       // This code sets the base URI for HTTP requests, 
       // and sets the Accept header to "application/json", 
       // which tells the server to send data in JSON format. 
       client.BaseAddress = new Uri("https://localhost:8443/"); 
       client.DefaultRequestHeaders.Accept.Clear(); 
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 


       // HTTP POST 
       var datatobeSent = new ApiSendData() 
            { 
             UserID = "xxxx", 
             Password = "yyyy", 
             ApiFunction ="NcrSecureData", 
             DppName ="CSampleCustomer", 
             DppVersion ="Latest", 
             ClearData ="1234567890", 
             ResultType = "JSON" 
            }; 

       HttpResponseMessage response = await client.PostAsJsonAsync("ncrApi", datatobeSent); 

       if (response.IsSuccessStatusCode) 
       { 
        // Get the URI of the created resource. 
        Uri ncrUrl = response.Headers.Location; 

        // do whatever you need to do here with the returned data // 


       } 
      } 
     } 
    } 
} 

In meiner Antwort Variable I die 200-OK http 1.1 Mitteilung erhalten, mit Content-Type = application/json und content-length = 174 ... aber keine tatsächlichen Daten empfangen ...

die Variable ncrUrl ist auch null ....

ich frage mich, ob ich weitere Anweisungen in meinem Konsolenprogramm benötigen, um die Daten zu empfangen?

Hier ist, was ich habe folgendes: - http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

+0

Was bedeutet das? 'string payload = await Antwort.Content.ReadAsStringAsync(); ' – Crowcoder

+0

{StatusCode: 200, ReasonPhrase:' OK ', Version: 1.1, Inhalt: System.Net.Http.StreamContent, Header: { X-Frame-Optionen : deny X-XSS-Schutz: 1; mode = Block X-Content-Type-Optionen: NOSNIFF Anschluss: schließen Accept-Rang: Bytes Datum: Mo, 25. Juli 2016 21.39.00 GMT ETag: "579686f4.174" Content-Length: 174 Content-Disposition: Anhang; filename = "results.json" Content-Type: application/json Zuletzt geändert: Mo, 25 Jul 2016 21:39:00 GMT – Philo

+0

Ich habe danach das Gefühl, dass aktuelle Daten von der API empfangen werden sollten. – Philo

Antwort

0

Nach dem Lesen der Kommentare scheint es, dass Ihre api so konfiguriert ist, wie JSON oder XML eine Datei statt einen String Inhalt zurückzukehren. Sie können HttpContent.ReadAsStreamAsync Method verwenden, um den Antwortstream zu lesen und in einer Datei zu speichern.

HttpResponseMessage response = await client.PostAsJsonAsync("ncrApi", datatobeSent); 

using (Stream output = File.OpenWrite("filename.txt")) // change 
{ 
    using (Stream input = await response.Content.ReadAsStreamAsync()) 
    { 
     input.CopyTo(output); 
    } 
} 
+0

Die Definition für GetResponseStream() ist nicht enthalten in HttpResponseMessage ... – Philo

+0

@Philo yeah tut mir leid, dass es zu lange her ist, dass ich HttpResponseMessage zuletzt verwendet habe. Siehe meine aktualisierte Antwort. Verwenden Sie stattdessen 'ReadAsStreamAsync()'. –

Verwandte Themen