locked
Http Response Content Length RRS feed

  • Question

  • User1063080413 posted

    Hi:

    I am working on an app for the Blackberry.  I would like to use their dynamic licensing scheme.  They require a response to a request that contains a license key.  Their documentation is as follows: 

    The request is as follows:
    POST /pathfromdeveloper HTTP/1.1
    Content-Type: application/www-url-encoded
    Content-Length: 120
    Host: hostfromdeveloper
    PIN=12341234&email=customeremail@email.com&product=product&version=1.2&transactionid=123&test=false


    The vendor portal expects the following response:
    HTTP/1.1 200 OK
    Content-Type: application/www-url-encoded
    Content-Length: 20
    key=ABCDEFGHIJK

    Looking at the above, I intended to do something like:

               String hkeystr = "somekindofkey";
                sb.AppendFormat("Content-Length: {0}\r\n", hkeystr);
                sb.AppendFormat("{0}={1}", "key", hkeystr);
                //now post back the key.
                Response.ContentType = "application/www-url-encoded";
                Response.Write(Server.UrlEncode(sb.ToString()));

     

    What I'm not sure about is the Content-Length part of this.  Content-Length is a standard HTTP header, but the response class does not have it as a member.  It has Content type, but not ContentLength.  Is what I did above correct, or am I to assume that the Response class will automatically include the Content-Length as part of the Response.Write?

    Thanks

    Tuesday, November 10, 2009 9:33 PM

Answers

  • User1566012831 posted

    The Content-Length field in the HTTP header is calculated and set for you automatically by the runtime.  You should never need to worry about it.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 10, 2009 11:08 PM

All replies

  • User1566012831 posted

    The Content-Length field in the HTTP header is calculated and set for you automatically by the runtime.  You should never need to worry about it.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 10, 2009 11:08 PM
  • User1063080413 posted

    Apparently, what I intended to do doesn't work and I don't have any idea what is wrong.  As described above, I need to send a response back, so this the complete response that I intended:

    protected void Page_Load(object sender, EventArgs e)
        {
            //now post back the key.
            String hashValue="someTypeOfKey";
            Response.ContentType = "application/www-url-encoded";
            Response.AddHeader("Key", hashValue);
            Response.Write(sb.ToString());
            Response.End();              

        } 

    What is getting returned is:

    HTTP/1.1 200 OK
    Content-Type: text/html; charset=utf-8;
    Content-Length: -1

    It does not appear that the Response values have been changed. 

    What am I omitting or doing wrong?

     

    Saturday, November 14, 2009 1:20 PM
  • User1566012831 posted

    You probably need to call Response.Flush() instead of Response.End().  I think the latter throws a ThreadAbortException that aborts the page.  Although, if you just let the page complete as it normally would, Flush() should be called for you; there shouldn't be a need to force it.

    Since you don't seem to have any markup, you might want to do this from a custom HttpModule (.ashx file) instead; it would be a little cleaner and more efficient.


    Saturday, November 14, 2009 8:10 PM
  • User1063080413 posted

    Thanks for the time about the .ashx file.  Pretty cool.  But it still doesn't work. Gives me exactly the same response.

    I will say that when I run it in the VS2008 debugger I get this response:

     Invalid at the top level of the document. Error processing resource 'http://localhost:53101/ESIAjaxSite/Mobile/StrokeWatchK...
     Key+%3d+17105
    ^

    I just assumed that it was because I called the .ashx page website start options and there really wasn't a page that for the response to return to.

    Might there be a setting on my server that I'm not aware of that is preventing the response from going out?

    I'm really stumped, it can't be that hard.

    thanks.

     

     

     

     

    Saturday, November 14, 2009 9:22 PM
  • User1566012831 posted

    Works for me.

    Here's my test code (test.ashx):

    <%@ WebHandler Language="C#" Class="test" %>
    
    using System;
    using System.Web;
    
    public class test : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            String hashValue = "someTypeOfKey";
            context.Response.ContentType = "application/www-url-encoded";
            context.Response.AddHeader("Key", hashValue);
            context.Response.Write("testing");
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }


    Here are the resulting HTTP headers (from Fiddler):

    HTTP/1.1 200 OK
    Cache-Control: private
    Content-Type: application/www-url-encoded; charset=utf-8
    Key: someTypeOfKey
    Date: Sun, 15 Nov 2009 03:25:51 GMT
    Content-Length: 7
    
    testing

    I'm using VS 2008 and .NET 3.5 on Vista with IIS 7.

    Saturday, November 14, 2009 10:29 PM
  • User1063080413 posted

    I believe it is working just fine from my debugging environment.  On my actual iis server, it doesn't seem to be behaving properly.  I'm thinking that IIS is messing something up.  The reason is if I just open IE and hit the page, it DOES come back as Content-Type: text/html; charset=utf-8 with no content.

    What is interesting is that it returns the master page for the website.  Is it possible that the master page is over-riding the response?

    I can send you the link to my page privately if you care to look.  Incidentally Fiddler is very cool.  I still need to figure out how to get it to watch my debugging server, but it is very helpful looking at what is happening to my real server.

    Thanks. 

    Sunday, November 15, 2009 7:42 AM
  • User1566012831 posted

    To get Fiddler to look at your dev server, just replace "localhost" in the URL with "ipv4.fiddler" -- leave the port number as-is.

    I don't understand why (or exactly when) you're seeing a Master Page.  Do you mean as part of the same response with the .ashx file?  I can't imagine how that's even possible.

    Can you tell anything from the Fiddler trace?


    Sunday, November 15, 2009 12:45 PM