我有这样的休息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
发布评论