我正在尝试对我的MVC 3 API进行非常基本的REST调用,并且我传入的参数不绑定到action方法.
客户
var request = new RestRequest(Method.POST);
request.Resource = "Api/Score";
request.RequestFormat = DataFormat.Json;
request.AddBody(request.JsonSerializer.Serialize(new { A = "foo", B = "bar" }));
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Run Code Online (Sandbox Code Playgroud)
服务器
public class ScoreInputModel
{
public string A { get; set; }
public string B { get; set; }
}
// Api/Score
public JsonResult Score(ScoreInputModel input)
{
// input.A and input.B are empty when called with RestSharp
}
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?
我正在尝试使用RestSharp来使用Web服务.到目前为止,一切都进行得很顺利(为约翰希恩和所有贡献者欢呼!)但我遇到了麻烦.假设我想以已经序列化的形式(即,作为字符串)将XML插入到我的RestRequest的主体中.是否有捷径可寻?看起来.AddBody()函数在场景后面进行序列化,所以我的字符串正在变成<String />.
任何帮助是极大的赞赏!
编辑:请求我当前代码的示例.见下文 -
private T ExecuteRequest<T>(string resource,
RestSharp.Method httpMethod,
IEnumerable<Parameter> parameters = null,
string body = null) where T : new()
{
RestClient client = new RestClient(this.BaseURL);
RestRequest req = new RestRequest(resource, httpMethod);
// Add all parameters (and body, if applicable) to the request
req.AddParameter("api_key", this.APIKey);
if (parameters != null)
{
foreach (Parameter p in parameters) req.AddParameter(p);
}
if (!string.IsNullOrEmpty(body)) req.AddBody(body); // <-- ISSUE HERE
RestResponse<T> resp = client.Execute<T>(req);
return resp.Data;
}
Run Code Online (Sandbox Code Playgroud) 我正在努力寻找一些使用RestSharp async和的异步C#代码的现代示例await.我知道Haack最近有更新,但我不知道如何使用新方法.
此外,我如何提供取消令牌以便可以取消操作(例如,如果一个人厌倦了等待并按下应用程序UI中的取消按钮).
我一直在尝试创建一个简单的原型Web应用程序,它使用RestSharp来调用Rest API.
我找不到一个很好的例子.有人可以分享并指导我找到合适的资源吗?我已经看过以下了,并没有提供我正在寻找的东西,即功能齐全的例子:
http://restsharp.org/(没有完整的应用示例)
http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/(似乎很旧)
在进行原型设计时,我得到以下代码的错误:
RestResponse response = client.Execute(request);
*Cannot implicitly convert type 'IRestResponse' to 'RestResponse'. An explicit conversion exists (are you missing a cast?) *
Run Code Online (Sandbox Code Playgroud) TL; DR版本
我有一个类型的对象JToken(但也可以是一个string),我需要将它转换为type变量中包含的类型:
Type type = typeof(DateTime); /* can be any other Type like string, ulong etc */
var obj = jsonObject["date_joined"]; /* contains 2012-08-13T06:01:23Z+05:00 */
var result = Some_Way_To_Convert(type, obj);
Run Code Online (Sandbox Code Playgroud)
上面result应该是一个DateTime对象,其值为date_joined.
全文
我在Windows Phone项目中使用RestSharp和Json.NET,并且在尝试从REST API反序列化JSON响应时遇到困难.
我实际上要完成的是编写一个通用方法,可以轻松地将我的JSON响应映射到我的CLR实体,就像你已经可以使用RestSharp一样.唯一的问题是默认的RestSharp实现对我不起作用,并且它无法成功解析JSON,因为响应并不总是返回所有属性(我不返回null来自REST服务器的字段).
这就是为什么我决定使用Newtonsoft的Json.NET,因为它有一个更强大的Json反序列化引擎.不幸的是,它不支持像RestSharp这样的模糊属性/字段名称(或者我还没有找到),所以当我使用像say这样的东西时,它也不能正确地映射到我的CLR实体JsonConvert.DeserializeObject<User>(response.Content).
这是我的Json的样子(实际上是一个例子):
{
"id" : 77239923,
"username" : "UzEE",
"email" : "uzee@email.net",
"name" : "Uzair Sajid",
"twitter_screen_name" : "UzEE",
"join_date" : "2012-08-13T05:30:23Z05+00",
"timezone" : 5.5,
"access_token" : {
"token" …Run Code Online (Sandbox Code Playgroud) 有什么可以让RestSharp忽略SSL证书中的错误吗?我有一个测试客户端,我连接的服务还没有一个有效的cetificate.
当我提出请求时,我收到错误:
The underlying connection was closed: Could not establish trust
relationship for the SSL/TLS secure channel.
Run Code Online (Sandbox Code Playgroud)
提前致谢
/迈克尔
更新:
我最终使用:
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => true;
Run Code Online (Sandbox Code Playgroud) 我正在RestSharp用来打电话给网络服务.一切都很好,但我想知道是否可以打印发送的原始请求标头和正文以及原始响应标头和返回的响应主体.
这是我创建请求并获得响应的代码
public static TResponse ExecutePostCall<TResponse, TRequest>(String url, TRequest requestData, string token= "") where TResponse : new()
{
RestRequest request = new RestRequest(url, Method.POST);
if (!string.IsNullOrWhiteSpace(token))
{
request.AddHeader("TOKEN", token);
}
request.RequestFormat = DataFormat.Json;
request.AddBody(requestData);
// print raw request here
var response = _restClient.Execute<TResponse>(request);
// print raw response here
return response.Data;
}
Run Code Online (Sandbox Code Playgroud)
那么,是否可以打印原始请求和响应?
有谁知道RestSharp RestClient 的默认超时值?
我一直无法做出明确的选择,并希望有人(或几个人的组合)可以指出使用RestSharp与ServiceStack的客户端服务之间的区别(请记住我已经在使用ServiceStack进行服务) .这是我到目前为止(仅差异).该列表相当小,因为它们确实非常相似:
我倾向于使用RestSharp,因为它更倾向于直接使用POCO和非常少的字符串操作,但我认为ServiceStack可能是可以接受的,可以获得更容易阅读的验证和代码.
那么,这里有一些问题:
我知道这不是一个完全主观的问题,但至少我正在寻找这个问题的答案(这是主观的):
我正在为RSS阅读服务建立一个客户端.我正在使用RestSharp库与他们的API进行交互.
API声明:
创建或更新记录时,必须设置application/json; charset = utf-8作为Content-Type标头.
这就是我的代码:
RestRequest request = new RestRequest("/v2/starred_entries.json", Method.POST);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.RequestFormat = DataFormat.Json;
request.AddParameter("starred_entries", id);
//Pass the request to the RestSharp client
Messagebox.Show(rest.ExecuteAsPost(request, "POST").Content);
Run Code Online (Sandbox Code Playgroud)
然而; 服务返回"错误415:请使用'Content-Type:application/json; charset = utf-8'标题." 为什么RestSharp没有传递标题?
restsharp ×10
c# ×6
rest ×3
.net ×2
async-await ×1
asynchronous ×1
http ×1
http-headers ×1
https ×1
json ×1
json.net ×1
rest-client ×1
servicestack ×1
timeout ×1
xml ×1