C # webapi POST ผ่านโปรแกรมคอนโซล

ฉันมีเว็บ API ที่ฉันสามารถเข้าถึงได้ผ่านเบราว์เซอร์: -

https://127.0.0.1:8443/ncrApi

ฉันกำลังพยายามสร้างโปรแกรมคอนโซลอย่างง่ายใน C# โดยใช้ VS2015 เพื่อส่งข้อมูลและรับการตอบกลับโดยใช้ http POST

นี่คือสิ่งที่ฉันมี: -

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 //


                }
            }
        }
    }
}

ในตัวแปรตอบกลับของฉัน ฉันได้รับข้อความ 200 OK http 1.1 โดยมี content type = application/json และ content-length = 174... แต่ไม่ได้รับข้อมูลจริง...

ตัวแปร ncrUrl ก็เป็นโมฆะเช่นกัน....

ฉันสงสัยว่าฉันต้องการคำสั่งเพิ่มเติมในโปรแกรมคอนโซลเพื่อรับข้อมูลหรือไม่

นี่คือสิ่งที่ฉันได้ติดตาม:- http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client


person Philo    schedule 25.07.2016    source แหล่งที่มา
comment
สิ่งนี้ให้ผลอะไร? string payload = await response.Content.ReadAsStringAsync();   -  person Crowcoder    schedule 26.07.2016
comment
{StatusCode: 200 ReasonPhrase: 'ตกลง' เวอร์ชัน: 1.1 เนื้อหา: System.Net.Http.StreamContent ส่วนหัว: { X-Frame-Options: ปฏิเสธ X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff การเชื่อมต่อ: ปิด ยอมรับ-ช่วง: ไบต์ วันที่: จันทร์ 25 กรกฎาคม 2559 21:39:00 GMT ETag: 579686f4.174 ความยาวของเนื้อหา: 174 การจัดการเนื้อหา: ไฟล์แนบ; filename=results.json ประเภทเนื้อหา: application/json แก้ไขล่าสุด: จันทร์ 25 กรกฎาคม 2559 21:39:00 GMT }}   -  person Philo    schedule 26.07.2016
comment
ฉันรู้สึกว่าหลังจากนี้ควรได้รับข้อมูลจริงจาก API   -  person Philo    schedule 26.07.2016
comment
ดูเหมือน JSON ดังนั้นฉันคิดว่ามันใช้งานได้ แต่การดำเนินการ ncrApi ของคุณอาจจะไม่ได้ทำในสิ่งที่คุณคิดว่ามันกำลังทำอยู่   -  person Crowcoder    schedule 26.07.2016
comment
แต่เมื่อฉันเข้าถึงมันจากเบราว์เซอร์... และให้ข้อมูลเดียวกัน มันทำให้ฉันดาวน์โหลดไฟล์ที่สามารถดาวน์โหลดได้ และไฟล์นั้นก็มีเนื้อหาที่ถูกต้องเช่นกัน   -  person Philo    schedule 26.07.2016
comment
ฉันรู้สึกว่าฉันได้รับเพียงส่วนแรกของข้อความ 200OK... และมีส่วนที่สองของข้อความที่มีข้อมูลเอาต์พุต... แต่ไม่แน่ใจว่าจะเข้าถึงได้อย่างไร   -  person Philo    schedule 26.07.2016


คำตอบ (1)


เมื่ออ่านความคิดเห็น ดูเหมือนว่า API ของคุณได้รับการกำหนดค่าให้ส่งคืนไฟล์แทนที่จะเป็นเนื้อหาสตริง เช่น JSON หรือ XML คุณสามารถใช้ วิธี HttpContent.ReadAsStreamAsync เพื่ออ่านสตรีมการตอบกลับและบันทึกลงในไฟล์

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);
    }
}
person jegtugado    schedule 25.07.2016
comment
คำจำกัดความของ GetResponseStream() ไม่มีอยู่ใน HttpResponseMessage... - person Philo; 26.07.2016
comment
@ Philo ใช่แล้ว ขอโทษด้วยที่มันนานเกินไปแล้วตั้งแต่ฉันใช้ HttpResponseMessage ครั้งล่าสุด ดูคำตอบที่อัปเดตของฉัน ใช้ ReadAsStreamAsync() แทน - person jegtugado; 27.07.2016