标签: restsharp

RestSharp RestRequest.AddBody不使用Newton.Json属性

var obj = new MyObject();
Run Code Online (Sandbox Code Playgroud)

我遇到RestSharp的问题RestRequest.AddBody(obj); 正确序列化对象.

class MyObject
{
   [JsonProperty(PropertyName="a")]
   public A{get;set;}

   [JsonProperty(PropertyName="b")]
   public B{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

问题是AddBody序列化器没有考虑我的JsonProperty属性,我似乎可以弄清楚如何在RestRequest或RestClient上设置序列化器?

c# restsharp

8
推荐指数
2
解决办法
6889
查看次数

显示错误的RestSharp无法创建接口实例必须手动反序列化

我有RestSharp(类似于HttpClient)从Web Api方法调用和返回数据

我收到了这个错误 {"Cannot create an instance of an interface."}

我的代码看起来像这样:

public List<Interest> GetInterests()
{
    var request = new RestRequest(apiPath + "GetInterests", Method.GET) { RequestFormat = DataFormat.Json };
    var response = Client.Execute<List<Interest>>(request);
    return response.Data;
}
Run Code Online (Sandbox Code Playgroud)

我的回答.内容如下所示.

现在问题似乎是DATA在响应对象的Content属性中作为JSON存在.我可以从那里手动反序列化它.但是之前是自动将其反序列化为Data属性,我不确定现在发生了什么.

如果我手动反序列化json,它可以工作 - 但为什么我需要这样做?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 
Run Code Online (Sandbox Code Playgroud)

响应:

"StatusCode: OK, Content-Type: application/json; charset=utf-8, Content-Length: 5597)"

JSON代码返回

[
   {
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":1,
      "InterestName":"Nursing",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   },
   {
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":2,
      "InterestName":"Creating/Supporting Networks of Community Partners", …
Run Code Online (Sandbox Code Playgroud)

c# json restsharp deserialization asp.net-web-api

8
推荐指数
1
解决办法
3303
查看次数

如何在单元测试中模拟RestSharp可移植库

我想模拟RestClient类进行测试

public class DataServices : IDataServices
{
    private readonly IRestClient _restClient;


    public DataServices(IRestClient restClient)
    {
        _restClient = restClient;
    }

    public async Task<User> GetUserByUserName(string userName)
    {
        User user = null;

        // create a new request
        var restRequest = new RestRequest("User", Method.GET);
        // create REST parameters
        restRequest.AddParameter("userName", userName, ParameterType.QueryString);
        // execute the REST request
        var restResponse = await _restClient.Execute<User>(restRequest);
        if (restResponse.StatusCode.Equals(HttpStatusCode.OK))
        {
            user = restResponse.Data;
        }
        return user;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的考试班:

[TestClass]
public class DataServicesTest
{
    public static IRestClient MockRestClient<T>(HttpStatusCode httpStatusCode, string …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq restsharp xamarin

8
推荐指数
1
解决办法
6492
查看次数

RestSharp方法引发System.Xml.XMlException“ =”是意外令牌。预期令牌为“;”

我查看了该问题的答案,发现无效字符会导致引发此错误的问题。我的问题有点不同,我使用RestSharp进行API调用,如下所示:

 private static T Execute<T>(IRestRequest request, string baseUrl) where T : class, new()
    {
        var client = new RestClient(baseUrl);
        var response = client.Execute<T>(request);

        if (response.ErrorException != null)
        {
            Console.WriteLine(
                "Error: Exception: {0}, Headers: {1}, Content: {2}, Status Code: {3}",
                response.ErrorException,
                response.Headers,
                response.Content,
                response.StatusCode);
        }

        return response.Data;
    }

 public static ProPayResponse MerchantSignUpForProPay()
    {
        var baseUrl = "https://xmltestapi.propay.com/ProPayAPI";
        var request = BuildMerchantTestData();
        var restRequest = CreateRestRequest("SignUp", Method.PUT);
        restRequest.AddJsonBody(request);
        return Execute<ProPayResponse>(restRequest, baseUrl);
    }

    private static async Task<RestRequest> CreateRestRequest(string resource, Method method) …
Run Code Online (Sandbox Code Playgroud)

c# json json.net restsharp

8
推荐指数
1
解决办法
281
查看次数

无法使用RestSharp发送cookie

我一直试图使用几种不同的方法在Windows Phone上访问基于REST的API,但我似乎遇到了将cookie附加到请求中的问题.我已经尝试过这种WebClient方法(现在似乎已经标记为SecurityCritical,因此您不能再继承它并添加代码).我简要地看了一下HttpWebRequest,看起来很麻烦.

现在我正在使用RestSharp,它似乎可以使用,但我仍然遇到问题,我的cookie没有被添加到请求发送时.

我的代码如下:

// ... some additional support vars ...
private RestClient client;

public ClassName() {
    client = new RestClient();
    client.BaseUrl = this.baseAddress.Scheme + "://" + baseAddress.DnsSafeHost;
}

public void GetAlbumList()
{
    Debug.WriteLine("Init GetAlbumList()");

    if (this.previousAuthToken == null || this.previousAuthToken.Length == 0) 
    {
        throw new MissingAuthTokenException();
    }

    RestRequest request = new RestRequest(this.baseUrl, Method.GET);

    // Debug prints the correct key and value, but it doesnt seem to be included
    // when I run the request
    Debug.WriteLine("Adding …
Run Code Online (Sandbox Code Playgroud)

.net c# windows-phone-7 restsharp

7
推荐指数
2
解决办法
7926
查看次数

使用RestSharp发布数据时序列化对象

我最近开始使用RestSharp来使用使用XML的REST服务.

它使反序列化从XML到自定义对象集合的对象变得微不足道.但我的问题是,在发回服务时,最好的重新排序方式是什么?

我应该使用LINQ-to-XML进行重新序列化吗?我尝试使用Serializeable属性和SerializeToXml实用程序函数,但是当我这样做时,似乎打破了RestSharp执行的反序列化.

.net c# rest web-services restsharp

7
推荐指数
1
解决办法
4813
查看次数

RestSharp在上传时将整个文件加载到内存中.怎么避免呢?

我在Mono项目中使用RestSharp来上传一些文件,我注意到当上传大文件时,内存会大大增加.

看看RestSharp源代码,我注意到FileParameter需要一个字节数组,这意味着它实际上是将文件加载到内存中.

难道我做错了什么?RestSharp有没有办法不这样做?我可能正在上传非常大的文件,所以从内存中上传它们不是一种选择.

欢迎提供任何帮助(包括告诉我使用单声道提供的另一个HTTP库).

.net c# mono http restsharp

7
推荐指数
2
解决办法
5935
查看次数

根据内容动态地将JSON反序列化为派生类型?

在我的小库中,我正在编写一个侧面项目,我正在使用RestSharp从Web API获取Json.对模型类的反序列化对于简单类型可以正常工作,但是在请求时有结果类型未知(或不清楚)的端点.

特别是它的GuildWars2 API v1和一个例子是项目数据.当然,每个项目都有基本属性,并根据查询的项目设置其他值.例如,武器有一些修饰符等等.

我的想法是为所有基本属性创建一个抽象的Item类,并从该类派生所有现有的子类型,添加所需的属性.我的问题是,如何决定要反序列化的子类.我已经阅读过各种涉及向Json字符串添加限定类型名称的方法,但由于我只从API获取数据,因此无法影响我的响应.

理想情况下,我总是希望反序列化到基类Item中,反序列化器通过Json字符串中包含的给定属性来决定实际类型.这是否可能?或者有更好的方法来解决这个问题吗?

提前致谢

编辑:这是武器的Json示例:

{
  "item_id": "30704",
  "name": "Twilight",
  "description": "",
  "type": "Weapon",
  "level": "80",
  "rarity": "Legendary",
  "vendor_value": "100000",
  "icon_file_id": "456031",
  "icon_file_signature": "CE3AF0B7B9BB6244726779F5B6A930541BA6C15F",
  "game_types": ["Activity", "Dungeon", "Pve", "Wvw"],
  "flags": ["HideSuffix", "NoSell", "SoulBindOnUse"],
  "restrictions": [],
  "weapon": {
    "type": "Greatsword",
    "damage_type": "Physical",
    "min_power": "995",
    "max_power": "1100",
    "defense": "0",
    "infusion_slots": [],
    "infix_upgrade": {
      "attributes": [
        {
          "attribute": "Power",
          "modifier": "179"
        },
        {
          "attribute": "Power",
          "modifier": "179"
        }
      ]
     },
     "suffix_item_id": "24599"
   }
}
Run Code Online (Sandbox Code Playgroud)

这将是一件盔甲:

{
  "item_id":"500",
  "name":"Carrion Conjurer …
Run Code Online (Sandbox Code Playgroud)

c# json restsharp deserialization

7
推荐指数
1
解决办法
2372
查看次数

如何让RestSharp使用BSON?

我正在使用RestSharp,并使用Json.NET进行序列化(请参阅此处).

Json.NET支持BSON,由于我的一些请求有大量的二进制数据块,我认为这会大大加快我的应用程序.但是,据我所知,RestSharp似乎没有任何内置的BSON支持.

使用Json.NET作为RestSharp的自定义序列化程序实现,因此乍一看它似乎可以修改该自定义序列化程序以使用BSON.但是,接口的Serialize方法RestSharp.Serializers.ISerializer返回string- 这是(我假设)不适合BSON.因此,我假设需要对RestSharp进行一些更重要的更改才能实现此更改.

有没有人想出办法做到这一点?


更新:我查看了RestSharp源代码,发现RestRequest.AddBody接受我的对象并将其序列化到请求体中的方法最终调用Request.AddParameter(使用序列化对象数据和参数类型RequestBody).

我想我可能能够将我的对象序列化为BSON,然后Request.AddParameter直接调用- 事实上我可以.但是,当RestSharp然后执行时RestRequest,它无法将二进制内容放入请求中,因为有关于请求内容的其他嵌入式假设是UTF-8编码的.

因此看起来这个hack不起作用 - 需要对RestSharp本身进行一些更改,而且我不是那个工作的人......


更新2:我决定使用调试器来计算我需要更改多少RestSharp以克服正文编码问题,所以我换掉了我的NuGet版本的RestSharp并将RestSharp项目包含在我的解.而且......它奏效了.

事实证明,在过去的几个月里,RestSharp已经发生了变化,而NuGet版本还没有.

因此,您现在可以使用AddParameter并传入已经BSON编码的对象,而RestSharp会将其发送到服务器而不会抱怨.

serialization json.net bson restsharp

7
推荐指数
1
解决办法
1011
查看次数

将单个实例维护到RestSharp客户端

此问题类似,是否可以实例化客户端并坚持下去,或者我是否需要为每个呼叫(或一批呼叫)创建一个新实例?

c# restsharp

7
推荐指数
2
解决办法
9987
查看次数