How to handle Compressed Request in WCF REST service - wcf

We have a WCF REST service hosted on IIS 7 with .NET Framework 4.5. The client is sending data in GZip compressed format with request headers:
Content-Encoding:gzip
Content-Type: application/xml
But we are getting bad request from the server, if the request is in compressed format. We enabled Request compression by implementation of IHttpModule that will filter/modify incoming requests. From my understanding, this is failing because WCF uses original content length (that of compressed data) instead of Decompressed data. So here are my questions:
Is there any way we can fix this content length issues in IIS7/.NET 4.5? My HTTP module implementation is given below:
httpApplication.Request.Filter = New GZipStream(httpApplication.Request.Filter, CompressionMode.Decompress)`
If fixing the content length issue is not possible at server side, is there any way I can send original content length from client with a compressed request? Client side implementation is as follows:
using (Stream requeststream = serviceRequest.GetRequestStream())
{
if (useCompression)
{
using (GZipStream zipStream = new GZipStream(requeststream, CompressionMode.Compress))
{
zipStream.Write(bytes, 0, bytes.Length);
zipStream.Close();
requeststream.Close();
}
serviceRequest.Headers.Add("Content-Encoding", "gzip");
}
else
{
requeststream.Write(bytes, 0, bytes.Length);
requeststream.Close();
}
}

Check if this can work for you Compression and the Binary Encoder
MSDN: Choosing a Message Encoder

Related

Restrict size of response from client side

Is there a way to restrict the size of the http response content while forming the http request? We use the following piece of code to execute a GET request and intend to handle responses of size <= 1MB
HttpUriRequest httpUriRequest = new HttpGet(this.getResolvedEndpoint());
HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
HttpResponse response = httpClient.execute(httpUriRequest, context);
HttpEntity entity = response.getEntity();
axios provides support for a parameter maxContentLength which helps in capping the size of the http response size. We are looking for an equivalent solution in JAVA.

Post request error when sending "application/octet-stream" to an ASP.NET Core Web API service

I need to create an ASP.NET Core 3 Web API that understand this URL
http://myapp.com/MyASPNetCore3WebApi/myController/myWebMethod?user=A0001
and one zipfile which goes as a content. This is the code that calls the needed API, which I need to create:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URI);
httpWebRequest.Timeout = -1;
httpWebRequest.KeepAlive = false;
httpWebRequest.Method = "POST";
httpWebRequest.ProtocolVersion = HttpVersion.Version10;
httpWebRequest.ContentType = "application/octet-stream";
httpWebRequest.Accept = "application/octet-stream";
httpWebRequest.ContentLength = data.Length;
Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
The code above is working fine, it is used everyday, sending data to a java web service, now I am replacing that system for a new one in ASP.NET Core and I can't change the caller's code, that's why I need to create a Web API that understand that URL.
I have wrote this code in my Web API, but I guess I am missing something that I canĀ“t figure it out because I get an error ion the client (code above)
[HttpPost("myWebMethod")]
public FileStreamResult myWebMethod(string user, [FromBody] Stream compress)
{
byte[] zip = ((MemoryStream)compress).ToArray();
byte[] data = ZipHelper.Uncompress(zip);
.....................
}
The error I get in the client is this:-
[System.Net.WebException] {"The remote server returned an error: (415)
Unsupported Media Type."} System.Net.WebException
Thanks in advance for any help
If the goal is to read the raw request content, this can be done using HttpContext controller property. HttpContext has Request property that provides access to the actual HTTP request.
No additional model properties or controller arguments are needed to access raw request stream. It's important to note that FromBody and FromForm binding should not be used in this case.
There are couple notes regarding the code in the example from the original question.
byte[] zip = ((MemoryStream)compress).ToArray();
byte[] data = ZipHelper.Uncompress(zip);
The HttpContext.Request.Body property does not return MemoryStream, it returns its own implementation of a Stream. It means that there is no ToArray method.
When reading the entire content of a request directly into the server's memory, it is better to check the content length, otherwise the client can crash the server by sending a large enough request.
Using *Async methods when reading the content of the request will improve performance.

WCF Gzip on POST Request

I have a WCF service with some post requests. I am using JSON for this WCF. Through the client for the WCF i need to send a GZipped/Compressed content to that POST method. How to achieve this?
You can use GZipStream class from System.IO.Compression namespace to compress your request data. Following sample code shows how you can compress it.
using (Stream requestStream = request.GetRequestStream())
{
using(var zipStream = new GZipStream(requestStream , CompressionMode.Compress))
{
zipStream.Write(byteData, 0, byteData.Length);
}
}

How to Send Byte Array in http request in Jmeter

I`m using JMeter for load testing where I have to call the upload Image API through an HTTP request, and to achieve this I have to convert an image into a compressed byte array to send it out as post data through an HTTP request.
Can anyone show me how it would be possible through JMeter?
Your help would really be appreciated.
There are several options on how you can proceed:
You can use HTTP Raw Request Sampler (available through JMeter Plugins site) which gives you full control on what, how and where you send.
Have you tried enabling Use multipart/form-data for POST for HTTP Request Sampler? This is how files should be uploaded as per RFC-1867.
If your use case is specific and none of the above is applicable, you can always use JMeter Scripting extensions. For example if you add a Beanshell Pre Processor to your HTTP Request which performs file upload with something like:
FileInputStream in = new FileInputStream("/home/glinius/401.png");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int i; (i = in.read(buffer)) != -1; ) {
bos.write(buffer, 0, i);
}
in.close();
byte[] imageData = bos.toByteArray();
bos.close();
vars.put("imageData", new String(imageData));
You'll be able to add ${imageData} parameter in your POST request.
Yes, I follow this method "add a Beanshell Pre Processor to your HTTP Request", and successful.
For my case, I also add a "HTTP Header Manager", specify: "Content-Encoding:gzip", "Content-Type:"application/x-www-form-urlencoded", "Accept:/".
And, set String encoding by: vars.put("binaryData", new String(binThrift, "ISO-8859-1"));
HTTP Header Manager
Beanshell Pre Processor
HTTP Request
Real Request

How To Set useUnsafeHeaderParsing For .NET Compact Framework

In my Windows CE 6.0 app, I am communicating with a proprietary web server device that is returning bad header information (more specifically, it's returning NO header information).
I believe this lack of header information is the reason why my HttpWebRequest methods are not working properly.
I recall that the .NET "regular" Framework allows for us to programmatically configure the System.Net.Configuration assembly to allow for invalid headers (useUnsafeHeaderParsing).
Unfortunately, for me, the System.Net.Configuration assembly is not included in the Compact Framework.
Is there a similar configuration in CF that is exposed that allows us to programmatically allow for invalid headers?
I was unable to find a work-around for setting the UseUnsafeHeaderParsing. I decided to remove the implementation of the HttpWebRequest class and use the TcpClient instead. Using the TcpClient class will ignore any problems that may exist with the HTTP Headers - the TcpClient doesn't even think in those terms.
Anyway, using the TcpClient I am able to get the data (including the HTTP Headers) from the proprietary web server that I mentioned in my original post .
For the record, here is a sample of how to retrieve data from a web server via the TcpClient:
The code below is essentially sending a client side HTTP Header packet to a web server.
static string GetUrl(string hostAddress, int hostPort, string pathAndQueryString)
{
string response = string.Empty;
//Get the stream that will be used to send/receive data
TcpClient socket = new TcpClient();
socket.Connect(hostAddress, hostPort);
NetworkStream ns = socket.GetStream();
//Write the HTTP Header info to the stream
StreamWriter sw = new StreamWriter(ns);
sw.WriteLine(string.Format("GET /{0} HTTP/1.1", pathAndQueryString));
sw.Flush();
//Save the data that lives in the stream (Ha! sounds like an activist!)
string packet = string.Empty;
StreamReader sr = new StreamReader(ns);
do
{
packet = sr.ReadLine();
response += packet;
}
while (packet != null);
socket.Close();
return (response);
}