Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从外部REST web服务获取记录_C#_Json_Rest_Console Application - Fatal编程技术网

C# 如何从外部REST web服务获取记录

C# 如何从外部REST web服务获取记录,c#,json,rest,console-application,C#,Json,Rest,Console Application,我需要从另一家公司的RESTWeb服务中获取数据,但我从未这样做过。我认为我走的是对的,但我甚至不确定这一点。我知道我丢失了完整的url(不知道应该放在哪里),如果我成功获得json结果,我不知道该怎么办。请帮忙 using System; using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.

我需要从另一家公司的RESTWeb服务中获取数据,但我从未这样做过。我认为我走的是对的,但我甚至不确定这一点。我知道我丢失了完整的url(不知道应该放在哪里),如果我成功获得json结果,我不知道该怎么办。请帮忙

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

namespace GetData
{
    public class ReceiptHeader
    {
        private string _company;
        private string _warehouse;

        public ReceiptHeader()
        { }

        public string Company
        {
            get { return _company; }
            set { _company = value; }
        }
        public string Warehouse
        {
            get { return _warehouse; }
            set { _warehouse = value; }
        }
    }

    class Program
    {
        static HttpClient client = new HttpClient();

        static void ShowHeader(ReceiptHeader crh)
        {
            Console.WriteLine($"Company: {crh.Company}\tWarehouse: " +
                $"{crh.Warehouse}");
        }

        static async Task<ReceiptHeader> GetRHAsync(string path)
        {
            ReceiptHeader HeaderInfo = null;
            HttpResponseMessage response = await client.GetAsync(path);
            if (response.IsSuccessStatusCode)
            {
                HeaderInfo = await response.Content.ReadAsAsync<ReceiptHeader>();
            }
            return HeaderInfo;
        }

        static void Main()
        {
            RunAsync().GetAwaiter().GetResult();
        }

        static async Task RunAsync()
        {
            // Update port # in the following line.
            client.BaseAddress = new Uri("https://externalwebaddress.com/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", "TOKEN");

            try
            {
                // Get the header
                crh = await GetRHAsync(url.PathAndQuery);
                ShowHeader(crh);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }
    }
}
使用系统;
使用System.Collections.Generic;
Net系统;
使用System.Net.Http;
使用System.Net.Http.Header;
使用System.Threading.Tasks;
命名空间GetData
{
公共类接收头
{
私人字符串公司;
私人字符串仓库;
公共接收头()
{ }
公共字符串公司
{
获取{return\u company;}
设置{u company=value;}
}
公共字符串仓库
{
获取{return\u warehouse;}
设置{u warehouse=value;}
}
}
班级计划
{
静态HttpClient=新HttpClient();
静态无效显示标头(接收标头crh)
{
Console.WriteLine($“公司:{crh.Company}\t安置所:”+
$“{crh.Warehouse}”);
}
静态异步任务GetRHAsync(字符串路径)
{
ReceiptHeader HeaderInfo=null;
HttpResponseMessage response=wait client.GetAsync(路径);
if(响应。IsSuccessStatusCode)
{
HeaderInfo=wait response.Content.ReadAsAsync();
}
返回HeaderInfo;
}
静态void Main()
{
RunAsync().GetAwaiter().GetResult();
}
静态异步任务RunAsync()
{
//在下一行中更新端口#。
client.BaseAddress=新Uri(“https://externalwebaddress.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
client.DefaultRequestHeaders.Authorization=
新的AuthenticationHeaderValue(“持票人”、“代币”);
尝试
{
//拿到头球
crh=等待GetRHAsync(url.PathAndQuery);
显示头(crh);
}
捕获(例外e)
{
控制台写入线(e.Message);
}
Console.ReadLine();
}
}
}

这是否回答了您的问题?发出REST请求是编程中一个非常常见的操作,而且肯定有人以前做过,这意味着这是在stackoverflow上。这很可能被认为是一个重复的问题。你应该看看这里: