csharp #c##webapi klient do mediqusa

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp #c##webapi klient do mediqusa相关的知识,希望对你有一定的参考价值。

using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using WebApi.Controllers;

namespace WebApiConsoleClient
{
    class Program
    {
        internal static string uri = @"http://172.18.53.75:8088/";
        static void Main(string[] args)
        {

            DoIt();
            Console.ReadLine();
        }

        private static async void DoIt()
        {

            using (var client = new HttpClient())
            {
                try
                {
                    client.DefaultRequestHeaders.Authorization =
                        new AuthenticationHeaderValue(
                            "Basic",
                            Convert.ToBase64String(
                                System.Text.Encoding.ASCII.GetBytes(
                                    string.Format("{0}:{1}", "test", "test"))));

                    Console.WriteLine("Dane pacjenta po NOPER");

                    HttpResponseMessage response = await PacjentByNoper(client);

                    Console.WriteLine("10 tys rekordów z danymi pacjentów");

                     response = await ListaPacjentow(client);

                    Console.WriteLine("Dane pacjenta po PESEL");

                    response = await PacjentByPesel(client);
                    Console.WriteLine("Wykaz badań pacjenta");

                    response = await BadaniaPacjenta(client);

                    Console.WriteLine("dodaj  pacjenta z wymuszonym noper= 37688");

                    response = await DodajPacjenta(client, 37688);

                    Console.WriteLine("usuń  pacjenta o noper= 37688");

                    response = await UsunPacjenta(client, 37688);

                    Console.WriteLine("dodaj  pacjenta i wylicz noper");

                    response = await DodajPacjenta(client);

                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ex.Message);
                    Console.ResetColor();
                }
            }
        }

        private static async Task<HttpResponseMessage> ListaPacjentow(HttpClient client)
        {
            var response = await client.GetAsync($"{uri}test");
            if (response.IsSuccessStatusCode)
            {
                var id = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Result from GET command: " + id);
            }

            return response;
        }

        private static async Task<HttpResponseMessage> DodajPacjenta(HttpClient client, int noper = 0)
        {
            var pacjent = new MperDto();

           // pacjent.Gbsno = 81170;
            pacjent.Noper = noper;
            pacjent.KartaPacjenta = "20338";
            pacjent.FirstName= "MIESZKO";
            pacjent.LastName = "PIERWSZY";
            pacjent.Pesel = "69000068815";
            pacjent.Ulica = "GRUNWALDZKA";
            pacjent.NrDomu = "912";
            pacjent.NrLokalu = "7";
            pacjent.Miejscowosc = "CHORZÓW";
            pacjent.KodPocztowy = "41100";
            pacjent.KodTerytorialny = 2474011;
            pacjent.NrTelefonu = "601055393";

            var json = JsonConvert.SerializeObject(pacjent);
            var stringContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

            var response = await client.PostAsync($"{uri}test/create", stringContent);
            if (response.IsSuccessStatusCode)
            {
                var id = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Noper dodanego pacjenta: " + id);
            }

            return response;
        }

        private static async System.Threading.Tasks.Task<HttpResponseMessage> UsunPacjenta(HttpClient client, int noper)
        {

          //   var stringContent = new StringContent("{ \"firstName\": \"Andy\" }", System.Text.Encoding.UTF8,   "application/json");
                var postContent = new StringContent($"{noper}", System.Text.Encoding.UTF8, "application/json");
          
            var response = await client.PostAsync($"{uri}test/Delete", postContent);
            if (response.IsSuccessStatusCode)
            {
                var id = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Result from POST command: " + id);
            }

            return response;
        }

        private static async System.Threading.Tasks.Task<HttpResponseMessage> BadaniaPacjenta(HttpClient client)
        {
            var response = await client.GetAsync($"{uri}test/mbad/107079");
            if (response.IsSuccessStatusCode)
            {
                var id = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Result from GET command: " + id);
            }

            return response;
        }

        private static async System.Threading.Tasks.Task<HttpResponseMessage> PacjentByPesel(HttpClient client)
        {
            var response = await client.GetAsync($"{uri}test/pesel/79022610445");
            if (response.IsSuccessStatusCode)
            {
                var id = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Result from GET command: " + id);
            }

            return response;
        }

        private static async System.Threading.Tasks.Task<HttpResponseMessage> PacjentByNoper(HttpClient client)
        {
            var endpoint = $"{uri}test/noper/107079";
            var response = await client.GetAsync(endpoint);
            if (response.IsSuccessStatusCode)
            {
                var id = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Result from GET command: " + id);
            }

            return response;
        }
    }
}

以上是关于csharp #c##webapi klient do mediqusa的主要内容,如果未能解决你的问题,请参考以下文章

csharp 自动WebAPI lowerCase序列化。

csharp RouteTestingExtensions仅限WebAPI

csharp 常见的webapi配置

csharp 标准脚手架读写WebApi控制器

csharp Web Api枚举验证属性。 #webapi,#enum

csharp 跨站点Json在MVC和WebAPI中