Search code examples
.netwcfrestiis-7.5

IIS 7.5 Restful service returns Error 411 - length required from .net client but not fiddler


I'm using a Microsoft Stack: Windows Server 2008 R2 / IIS 7.5; .NET 4.0

Setting up the service on localhost, the client call the operation without a hitch. Setting up the service (originally as a Post then as a Get) I get the following error from .NET (method below). If I make the call directly through fiddler everything works and the (RAW) header is as follows:

HTTP/1.1 200 OK Cache-Control: private Content-Length: 19854411 Content-Type: application/xml; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET

If I make a call through console app or windows service I get the following error "The remote server returned an error: <411> Length Required."

I have a web service that is supposed to return a zip file after doing some work

    [OperationContract]
    [WebInvoke(UriTemplate = "/GetFiles/{profileLocationId}/{batchSize}", Method = "GET", RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    public byte[] ProfileImagesByProfileId(string profileLocationId, string batchSize)
    {
        //  var watch = new Stopwatch();
        //  watch.Start();
        try
        {
            long id = 0;
            long.TryParse(profileLocationId, out id);

            int size = 200;
            int.TryParse(batchSize, out size);

            var profileRoot = ConfigurationManager.AppSettings["ProfileImageRootDirectory"];
            var fileType = ConfigurationManager.AppSettings["ProfileImageDefaultFileType"];


            var packer = new ImagePackager(profileRoot, fileType);
            if (profileLocationId != null)
            {
                var stream = packer.CompressImages(id, size);

                return stream;
            }
        }
        catch (Exception ex)
        {
            return new byte[0];

        }

        return new byte[0];
    }
}

===========================

client method

 static void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        var id = GetLastProfileId();

       // var uri = string.Format("http://{0}/TwitterService/ProfileImages/{1}/{2}", _ipAddress, id, _batchsize);
        var uri = string.Format("http://{0}/Twitter_api/TwitterService/ProfileImages/{1}/{2}", _ipAddress, id, _batchsize);
        GetProfileImagesFromService(uri);
    }


    private static void GetProfileImagesFromService(string uri)
    {
        try
        {
            Console.WriteLine("Process started: {0}", DateTime.Now.ToLongTimeString());
            var request = WebRequest.Create(uri);
            request.Method = "GET";
            request.Timeout = 600000;
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                Console.WriteLine(response.StatusDescription);
                var dataStream = response.GetResponseStream();
                var reader = new StreamReader(dataStream, Encoding.UTF8);
                //  var responseFromServer = reader.ReadToEnd();

                var fileName = string.Format(@"{0}\{1:yyMMddhhmmss}.zip", _targetDirectory, DateTime.UtcNow);
                // File.WriteAllText(fileName, responseFromServer);
                // var xml = XDocument.Parse(responseFromServer);
                var xml = XDocument.Load(reader);
                var base64 = xml.Root.Value.ToCharArray();

                var byteArray = Convert.FromBase64CharArray(base64, 0, base64.Length);

                File.WriteAllBytes(fileName, byteArray);
                Console.WriteLine("GetProfile cycle ended: {0}", DateTime.Now.ToLongTimeString());
            }
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }
    }

Any guidance would be extremely appreciated.

Thank you in advance.


Solution

  • HTTP Error Code 411 means

    The server refuses to accept the request without a defined Content- Length. The client MAY repeat the request if it adds a valid Content-Length header field containing the length of the message-body in the request message.

    So if you are invoking HTTP POST method without any body then just add Content-Length:0 in your request object in header.