如何使用C#访问rest api [英] How can access rest api using C#
问题描述
我有这样的休息api url
https://example.com/ProductAPI/api/V1/getproduct 。
这个api有api_key,用户名和密码。如何使用c#访问此api和getproduct详细信息。
将从另一个URL获取的令牌
https:// example。 com / ProductAPI /通过用户名,密码,client_key和generictype
我尝试过:
WebRequest request = WebRequest.Create(@https://example.com/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
https://example.com/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
https://example.com/ProductAPI/ by passing username,password,client_key and generictype
What I have tried:
WebRequest request = WebRequest.Create(@"https://example.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!