我想压缩从客户端发送的请求。ServiceStack客户端压缩
我已经找到了Q/A: ServiceStack - How To Compress Requests From Client
但是使用这个代码时,我从服务器获取一个SerializationException内容应该与开始“{”而不是“\ u001F ......”
该解决方案是否仍然有效,或者有另一种方法来压缩客户端请求负载?
更新1:这是来自Fiddler的输出。要求:
POST http://xxxxxx:8104/entries HTTP/1.1
Accept: application/json
User-Agent: ServiceStack .NET Client 4,51
Accept-Encoding: gzip,deflate
Content-Encoding: gzip
Content-Type: application/json
Host: xxxxxx:8104
Content-Length: 187
Expect: 100-continue
[binary data not shown here]
和响应:
HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Tue, 03 Jan 2017 07:22:57 GMT
427
{"ResponseStatus":{"ErrorCode":"SerializationException","Message":"Could not deserialize 'application/json' request using Namespace.NameOfDto'\nError: System.Runtime.Serialization.SerializationException: Type definitions should start with a '{', expecting serialized type 'NameOfDto', got string starting with: \u001F \b\u0000\u0000\u0000\u0000\u0000\u0004\u00005 \n @\u0010 e 1 P W :h\u001D :D M'YЙ \u001D B| F 7 \r\n at ServiceStack.Text.Common.DeserializeTypeRefJson.StringToType(TypeConfig typeConfig, String strType, EmptyCtorDelegate ctorFn, Dictionary`2 typeAccessorMap)\r\n at ServiceStack.Text.Common.DeserializeType`1.<>c__DisplayClass1_0.<GetParseMethod>b__1(String value)\r\n at ServiceStack.Text.JsonSerializer.DeserializeFromString(String value, Type type)\r\n at ServiceStack.Text.JsonSerializer.DeserializeFromStream(Type type, Stream stream)\r\n at ServiceStack.Serialization.JsonDataContractSerializer.DeserializeFromStream(Type type, S
27e
tream stream)\r\n at ServiceStack.Host.Handlers.ServiceStackHandlerBase.CreateContentTypeRequest(IRequest httpReq, Type requestType, String contentType)","StackTrace":" at ServiceStack.Host.Handlers.ServiceStackHandlerBase.CreateContentTypeRequest(IRequest httpReq, Type requestType, String contentType)\r\n at ServiceStack.Host.RestHandler.CreateRequest(IRequest httpReq, IRestPath restPath, Dictionary`2 requestParams)\r\n at ServiceStack.Host.RestHandler.CreateRequest(IRequest httpReq, IRestPath restPath)\r\n at ServiceStack.Host.RestHandler.ProcessRequestAsync(IRequest httpReq, IResponse httpRes, String operationName)"}}
0
客户:
public class GzipJsonServiceClient : JsonServiceClient
{
public GzipJsonServiceClient()
{
SetRequestFilter();
}
public GzipJsonServiceClient(string baseUri)
: base(baseUri)
{
SetRequestFilter();
}
public override void SerializeToStream(IRequest requestContext, object request, Stream stream)
{
using (var gzipStream = new GZipStream(stream, CompressionMode.Compress))
{
base.SerializeToStream(requestContext, request, gzipStream);
gzipStream.Close();
}
}
private void SetRequestFilter()
{
RequestFilter = req =>
{
if (req.Method.HasRequestBody())
{
req.Headers.Add(HttpRequestHeader.ContentEncoding, CompressionTypes.GZip);
}
};
}
}
请求代码:
var client = new GzipJsonServiceClient(uri) { Timeout = TimeSpan.FromSeconds(10) };
var request = new NameOfDto();
client.Post(request);
服务端是从Visual Studio模板,托管ServiceStack Windows内部的服务服务。这是相当香草,用一个方法,该方法没有达到:
public void Post(NameOfDto request)
{
var appHost = (AppHost)HostContext.AppHost;
...
}
您可以更新您的问题以显示您使用的代码以及使用Fiddler之类的原始HTTP请求/响应头。 – mythz