如何使用C#访问rest api

编程入门 行业动态 更新时间:2024-10-08 00:34:45
本文介绍了如何使用C#访问rest api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的休息api url example/ProductAPI/api/V1/getproduct 。 这个api有api_key,用户名和密码。如何使用c#访问此api和getproduct详细信息。 将从另一个URL获取的令牌 https:// example。 com / ProductAPI /通过用户名,密码,client_key和generictype 我尝试过: WebRequest request = WebRequest.Create(@example/ProductAPI/api/V1/getproduct); request.Method =GET; request.Headers.Add(api_key,5g13441f-6915-4a34-8a53-bcb4er88b554); request.Headers [授权] =基本+ Convert.ToBase64String(Encoding.Default.GetBytes(admin:123)); request.ContentLength = 0; request.ContentType = @application / json; charset = utf-8; HttpWebResponse响应=(HttpWebResponse)request.GetResponse(); string jsonResponse = string.Empty; using(StreamReader sr = new StreamReader(response.GetResponseStream())) { jsonResponse = sr.ReadToEnd(); Console.WriteLine(jsonResponse); Console.ReadLine( ); }

I have rest api url like this example/ProductAPI/api/V1/getproduct. This api has api_key,username and password.How can I access this api and getproduct details using c#. The token which will get from another url example/ProductAPI/ by passing username,password,client_key and generictype What I have tried: WebRequest request = WebRequest.Create(@"example/ProductAPI/api/V1/getproduct"); request.Method = "GET"; request.Headers.Add("api_key","5g13441f-6915-4a34-8a53-bcb4er88b554"); request.Headers["Authorization"] = "Basic" + Convert.ToBase64String(Encoding.Default.GetBytes("admin:123")); request.ContentLength = 0; request.ContentType = @"application/json; charset=utf-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string jsonResponse = string.Empty; using (StreamReader sr = new StreamReader(response.GetResponseStream())) { jsonResponse = sr.ReadToEnd(); Console.WriteLine(jsonResponse); Console.ReadLine(); }

推荐答案

请参阅以下链接 link1 link2 我希望它适合你。 Refer following link link1 link2 I hope it will work for you.

更多推荐

如何使用C#访问rest api

本文发布于:2023-11-28 11:22:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   rest   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!