我想在POST某个地方使用RestSharp .我直接发布JSON(而不是POCO).因为我发布了普通的JSON,我相信我需要使用这种解决方法而不是设置Body:
request.AddParameter(
"application/json", myJsonString, ParameterType.RequestBody);
Run Code Online (Sandbox Code Playgroud)
当我没有压缩JSON时,这很好用.当我这样做时,使用这个:
request.Headers.Add("Content-Encoding", "gzip");
request.AddParameter(
"application/json",
GZipStream.CompressString(myJsonString),
ParameterType.RequestBody);
Run Code Online (Sandbox Code Playgroud)
这不起作用.我逐步完成了代码RestClient::ConfigureHttp,我看到了:
http.RequestBody = body.Value.ToString();
Run Code Online (Sandbox Code Playgroud)
因为我给出了一个字节数组,body.Value所以设置为System.Byte[]
有没有办法让RestSharp在POST请求中处理gzip压缩的json字符串?
我正在使用restsharp并遇到了问题.由于我使用google api返回xml数据,因此我遇到名称冲突.
例如,返回来自google contact api的所有"group"和"all contacts"都有一个"feed"的根节点,但其中包含不同的数据.
所以我这样做了
[XmlRoot(ElementName = "feed")]
public class GroupFeed
{
public string Id { get; set; }
public DateTime Updated { get; set; }
public string Title { get; set; }
public int TotalResults { get; set; }
public int StartIndex { get; set; }
public int ItemsPerPage { get; set; }
[XmlElement(ElementName="Entry")]
public List<GroupEntry> Entries { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
通过使用XmlRoot属性,它在我使用restsharp时起作用,但它从不填充Entries,即使它们是数据.
[XmlRoot(ElementName = "entry")]
public class GroupEntry
{
public string Id { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有一个Web API服务,我试图通过控制台使用RestSharp访问.我的RestSharp代码如下所示:
RestClient client = new RestClient(baseUrlString);
RestRequest request = new RestRequest("controllername/actionname");
request.RequestFormat = DataFormat.Json;
ProcessQuestion model = new ProcessQuestion()
{
Id = "123456",
InstanceId = "123",
UniqueId = "bfb16a18-d0d6-46ab-a5b3-2f0ebbfe8626",
PostedAnswer = new Dictionary<string, string>() { { "question_7907_1", "selected" }, { "question_7907_2", "selected" } }
};
request.AddBody(model);
var response = client.Execute(request)
Run Code Online (Sandbox Code Playgroud)
我的Web API操作采用与上述模型具有相同参数的模型.调用执行时,绑定失败,参数为null.我怀疑这是由于RestRequest.AddBody方法将application/json预先添加到body值,如下所示:
{application/json={"Id":"123456","InstanceId":"123","UniqueId":"bfb16a18-d0d6-46ab-a5b3-2f0ebbfe8626","PostedAnswer":{"question_7907_1":"selected","question_7907_2":"selected"}}}
Run Code Online (Sandbox Code Playgroud)
如果我使用Fiddler发布,身体会正确地绑定到模型.以下是我在Fiddler中提供的身体价值:
{'Id':'123456','InstanceId':'123','Uniqueid':'bfb16a18-d0d6-46ab-a5b3-2f0ebbfe8626','PostedAnswer':{'question_7907_1':'selected','question_7907_2':'selected'}}
Run Code Online (Sandbox Code Playgroud)
请注意,fiddler中的body值是相同的,除了preing application/json键.
还要注意:我已经尝试过似乎所有东西......我已经在动作中分离了参数,使用了FromBody和FromUri属性,尝试了自定义DictionaryModelBinder,尝试了自定义ValueBinders,试图改变我使用RestSharp的方式(带有RequestBody参数的AddParameter,AddObject,不同的URL样式等).
有没有其他人遇到这个,如果有的话,你解决了吗?我为这项服务选择了Web API,希望模型绑定能像在MVC中那样工作,但我看到的并非如此.
谢谢
编辑(已解决):RestSharp自动将JsonSerializer用于AddBody方法中传递的对象.我想我错过了一些简单的东西,事实上我是......将Method.Post参数添加到RestRequest实例化解决了这个问题.
我有这样的代码:
using System;
using RestSharp.Serializers;
public class Program
{
public static void Main()
{
var obj = new Order();
obj.Test = 42;
var serializer = new JsonSerializer();
var json = serializer.Serialize(obj);
Console.WriteLine(json);
}
}
public class Order
{
[SerializeAs(Name = "object")]
public string Object
{
get { return "Order"; }
}
[SerializeAs(Name = "TestName")]
public int Test
{
get;set;
}
}
Run Code Online (Sandbox Code Playgroud)
基于SerializeAs属性,RestSharp应使用属性中的名称,而不是属性名称.但它只是忽略了它.此代码的输出是:
{
"Object": "Order",
"Test": 42
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么,或者它与RestSharp不兼容?
DotNetFiddle中的相同代码片段 - http://dotnetfiddle.net/ffaXUY
我正在尝试使用"rest sharp"软件包在"xamarin studio"中获取数据,如下所示:
var client = new RestClient ("https://xxxx.xxxx.com");
var request = new RestRequest ("/api/external/xxxx/xxxx/", Method.GET);
request.AddParameter("username", "xxxx");
request.AddParameter("api_key", "xxxx");
request.AddHeader("Content-type", "application/json");
request.AddHeader("Accept", "text/plain");
request.RequestFormat = DataFormat.Json;
var response = client.Execute (request);
var model = JsonConvert.DeserializeObject<RootObject> (response.Content);
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
错误:SendFailure(写入标题时出错)
我真的不知道出了什么问题.我正在使用Windows 7 64位.
请帮忙...谢谢你提前!!!
我使用ResponseWriter来装载使用RestSharp的流.
var client = new RestClient
var request = new RestRequest();
// ...
request.ResponseWriter = (ms) => {
// how to detect the status code
};
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
如何在ResponseWriter中找到HTTP状态代码?有没有更好的下载流的方法?
我们有这样的场景:外部API根据请求是成功还是失败返回User XML或Error XML.
目前我正在将用户POCO传递给restsharp并正常工作.但如果失败,则此对象为NULL.除非我们手动解析Error XML,否则我们不知道失败的原因.
有办法解决这个问题吗?
例如
var restClient = new RestClient(baseURL);
var request = new RestRequest(uri);
request.Method = Method.POST;
var response = restClient.Execute<User>(request);
Run Code Online (Sandbox Code Playgroud)
在执行上述方法时,API可以返回Error xml对象.如何在失败时获取Error对象,在成功时获得User?
我正在尝试使用RestSharp和C#使用REST API.我正在使用的API文档提供了一个示例XML请求:
<?xml version='1.0' encoding='UTF-8'?>
<messages>
<accountreference>EX0000000</accountreference>
<from>07700900654</from>
<message>
<to>07700900123</to>
<type>SMS</type>
<body>Hello Mr Sands.</body>
</message>
<message>
<to>07700900124</to>
<type>SMS</type>
<body>Hello Mr Mayo.</body>
</message>
</messages>
Run Code Online (Sandbox Code Playgroud)
我正在努力了解如何以他们想要的格式构建请求(多个元素称为"消息")
我已经为RestSharp创建了这些类来序列化:
public class messages
{
public string accountreference { get; set; }
public string from { get; set; }
public message message { get; set; }
}
public class message
{
public string to { get; set; }
public string body { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的RestSharp代码:
var client = new RestClient("http://api.url.com/v1.0")
{
Authenticator =
new …Run Code Online (Sandbox Code Playgroud) 我从我的ASP.NET核心应用程序向第三方API发出Web请求.
通过RestSharp客户端从Hangfire定期作业向Asana API端点(HTTPS)发出请求.所有自己的应用程序页面都可以通过IIS获得,但应用程序无法提出任何请求.
我应该在哪里查看和调试什么来解决这个问题?
我正在使用RestSharp.RestClient(105.2.4-rc4-24214-01)开发.net核心应用程序.我设置
RestClient.Timeout=1
Run Code Online (Sandbox Code Playgroud)
要么
RestClient.Timeout=10000
Run Code Online (Sandbox Code Playgroud)
然后调用我的测试API
var tcs = new TaskCompletionSource<IRestResponse>();
RestClient.ExecuteAsync(request, response => { tcs.SetResult(response); })
return tcs.Task.Result;
Run Code Online (Sandbox Code Playgroud)
但它仍然使用默认值100000并生成"任务被取消".异常仅在100000毫秒之后.
我怎样才能改变这个值?