使用RestSharp获取JSON响应 [英] Get JSON response using RestSharp

查看:556
本文介绍了使用RestSharp获取JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我正在尝试使用RestSharp从REST请求中获取JSON响应; 我要执行的请求是以下请求:"http://myurl.com/api/getCatalog?token=saga001".如果在浏览器中执行它,效果很好.

I'm new to C# and I'm trying to get the JSON response from a REST request using RestSharp; The request I want to execute is the following one : "http://myurl.com/api/getCatalog?token=saga001". It works great if I'm executing it in a browser.

我已经尝试过了:

var client = new RestClient("http://myurl.com/api/");

var request = new RestRequest("getCatalog?token=saga001"); 

var queryResult = client.Execute(request);

Console.WriteLine(queryResult);

我得到的是"RestSharp.RestReponse",而不是我希望得到的JSON结果.

And I get "RestSharp.RestReponse" instead of the JSON result I'm hopping for.

感谢您的帮助!

推荐答案

尝试:

var client = new RestClient("http://myurl.com/api/");

var request = new RestRequest("getCatalog?token={token}", Method.GET); 

request.AddParameter("token", "saga001", ParameterType.UrlSegment);   

// request.AddUrlSegment("token", "saga001"); 

request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

var queryResult = client.Execute(request);

Console.WriteLine(queryResult.Content);

这篇关于使用RestSharp获取JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆