RestSharp 压缩请求,同时对服务器进行休息调用 [英] RestSharp compress request while making rest call to server

查看:66
本文介绍了RestSharp 压缩请求,同时对服务器进行休息调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 C# 客户端中使用了一个用 java 编写的 rest api.我正在向服务器输送大量数据,并且我正在使用 RestSharp.dll 进行休息调用.我要做的是构造一个对象并将其直接添加到 RestSharp 请求对象的主体中,并为其提及 .netserializer.所以它会自动序列化并发布它.一切都很好.

I am consuming a rest api written in java in my C# client. I was pumping hell lot of data to server and I was using RestSharp.dll for the purpose of making rest calls. What I will do is construct an object and add that directly in body of RestSharp request object and mention .netserializer for it. So it will automatically serialize it and post it. All works great.

现在我需要应用 LZO 或 GZIP 压缩.这样服务器的负载就会减少.

Now I need to apply LZO or GZIP compression. So that server will have less load.

我知道如何实现这两种压缩技术.但是如何用RestSharp请求对象来实现呢?

I know how to implement both compression techniques. But how to implement it with RestSharp request object?

我必须做的.我正在向请求正文添加对象.何时执行压缩?

What I got to do. I am adding object to body of request. When to perform compression?

任何帮助...

谢谢

推荐答案

RestSharp 从版本 102.7 开始支持 HTTP 压缩.我不确定这是否仅适用于 Windows Phone 或所有平台都支持压缩.

RestSharp supports HTTP compression since version 102.7. I'm not sure if this works only for Windows Phone or compression is supported for all platforms.

最常见的算法是 GZip 和 Deflate,尽管实际的压缩级别通常由服务器控制.

The most common algorithms are GZip and Deflate, although the actual level of compression is usually controlled by the server.

更新:

我刚刚使用 Fiddler 验证了 HTTP 压缩在 103.1 中默认启用.以下代码返回使用 GZip 编码的页面:

I just verified using Fiddler that HTTP Compression is enabled by default in 103.1. The following code returns a page encoded using GZip:

        var target = "http://msdn.microsoft.com/";
        var client=new RestClient(target);
        var request = new RestRequest("",Method.GET);
        var response = client.Execute(request);
        Console.WriteLine(response.Content);

无需添加 Accept-Encoding 标头.

There is no need to add the Accept-Encoding header.

压缩只有在服务器支持时才有效.否则响应将被解压缩.

Compression will only work if the server supports it. Otherwise the response will be uncompressed.

这意味着 Java 服务也必须启用 HTTP 压缩.

This means that the Java service will have to enable HTTP compression as well.

如果响应被压缩,则 Content-Encoding 标头将设置为压缩方法,例如.gzip.

If the response is compressed the Content-Encoding header will be set to the compression method, eg. gzip.

要直观地检查这一点,请打开 Fiddler 并检查对 RestRequest 的响应.响应窗格的转换器选项卡显示压缩设置.

To check this visually, open Fiddler and check the responce to the RestRequest. The Transformer tab of the Response pane displays the compression settings.

这篇关于RestSharp 压缩请求,同时对服务器进行休息调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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