I am new to Web API, and I have built an experiment REST Web Service using WCF Web API, and client is an Android device consuming the web service through Json objects. All is working great.
Now I want to encrypt/compress the content payload that web service returns. So I added another custom DelegatingHandler to do the compression (for now only compression, I will add encryption later), basically I am following Kiran Challa's excellent post:
It is working great, except the response is now in XML (compressed XML string, in Java, after I de-compressed it, I get a XML string). I could not figure out how to change it so that I get a compressed Json string, I tried to set the Content-Type of the
Headers to "application/json" by the following code in the CompressedContent constructor.
this.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
I did not work.
My service's method is decorated with WebMessageFormat.Json:
If comment out the addHeader("Accept-Encoding", "gzip") line in java code, my WCF REST web service will return the result in Json, but of course not compressed, but once compressed, the result string is XML.
I guess it is because in the CompressedContent class, in SerializeToStreamAsync() method, the stream's conent is in XML, and I am compressing the XML content. How can I make it so that the the stream is in JSON?
For SSL, one of the requirements is to use http only.
And for letting the web server do the compression, could you please point to me an example of that?
But I really want to do is encrypt the payload content, currently I am just learning how to manipulate the return response by following the compression example.
For SSL, one of the requirements is to use http only.
Doing this on your own is duplicating what SSL already provides. It's a lot of work and very error prone. That's why we use SSL -- smarter people have already built this infrastructure, so why not use it?
WenbiaoLiang
Member
2 Points
16 Posts
DelegatingHandler and force Json response
Mar 29, 2012 04:27 PM|LINK
Hi Guys,
I am new to Web API, and I have built an experiment REST Web Service using WCF Web API, and client is an Android device consuming the web service through Json objects. All is working great.
Now I want to encrypt/compress the content payload that web service returns. So I added another custom DelegatingHandler to do the compression (for now only compression, I will add encryption later), basically I am following Kiran Challa's excellent post:
http://forums.asp.net/post/4841854.aspx
It is working great, except the response is now in XML (compressed XML string, in Java, after I de-compressed it, I get a XML string). I could not figure out how to change it so that I get a compressed Json string, I tried to set the Content-Type of the Headers to "application/json" by the following code in the CompressedContent constructor.
this.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
I did not work.
My service's method is decorated with WebMessageFormat.Json:
[WebGet(UriTemplate = "blah/blah/blah", ResponseFormat = WebMessageFormat.Json)]
And my android Java code has set the Accept/Content-Type headers to "application/json", and "Accept-Encoding" to "gzip".
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Accept", "application/json");
httpget.addHeader("Content-Type", "application/json");
httpget.addHeader("Accept-Encoding", "gzip");
If comment out the addHeader("Accept-Encoding", "gzip") line in java code, my WCF REST web service will return the result in Json, but of course not compressed, but once compressed, the result string is XML.
I guess it is because in the CompressedContent class, in SerializeToStreamAsync() method, the stream's conent is in XML, and I am compressing the XML content. How can I make it so that the the stream is in JSON?
Thanks!
Wenbiao
BrockAllen
All-Star
27532 Points
4906 Posts
MVP
Re: DelegatingHandler and force Json response
Mar 29, 2012 04:32 PM|LINK
For compression: why not let the web server do it?
For encryption: why not let SSL do it?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
WenbiaoLiang
Member
2 Points
16 Posts
Re: DelegatingHandler and force Json response
Mar 29, 2012 04:48 PM|LINK
Thanks BrockAllen,
For SSL, one of the requirements is to use http only.
And for letting the web server do the compression, could you please point to me an example of that?
But I really want to do is encrypt the payload content, currently I am just learning how to manipulate the return response by following the compression example.
Thanks again!
WenbiaoLiang
Member
2 Points
16 Posts
Re: DelegatingHandler and force Json response
Mar 29, 2012 04:48 PM|LINK
sorry duplicate post
BrockAllen
All-Star
27532 Points
4906 Posts
MVP
Re: DelegatingHandler and force Json response
Mar 29, 2012 04:57 PM|LINK
Compression in IIS7
Doing this on your own is duplicating what SSL already provides. It's a lot of work and very error prone. That's why we use SSL -- smarter people have already built this infrastructure, so why not use it?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
WenbiaoLiang
Member
2 Points
16 Posts
Re: DelegatingHandler and force Json response
Mar 29, 2012 06:31 PM|LINK
Thanks!
WenbiaoLiang
Member
2 Points
16 Posts
Re: DelegatingHandler and force Json response
Mar 29, 2012 06:33 PM|LINK
I have figured it out.
In the derived CompressedContent class' method:
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
the originalContent.Headers.ContentType has been reset to "application/xml" for some reason. I have to set it to "application/json" manually.
Once I did that, the compressed string is Json.
Cheers,