Error when setting "Host" HTTP Header

Last post 10-15-2009 12:32 PM by randallt. 3 replies.

Sort Posts:

  • Error when setting "Host" HTTP Header

    10-13-2009, 11:07 AM
    • Member
      point Member
    • johnsheehan
    • Member since 10-13-2009, 3:05 PM
    • Posts 2

    Services like S3 heavily rely on the value of the "Host" http header, but when I set that value in DefaultHeaders I get an exception thrown when the request is executed (the exception text just says "Host"). Same with the Date header. Is there a supported way to set these headers that I'm missing?

  • Re: Error when setting "Host" HTTP Header

    10-14-2009, 11:57 AM
    • Member
      90 point Member
    • randallt
    • Member since 02-11-2009, 11:53 AM
    • Posts 30

    I assume you are using the HttpClient and trying to set the "Host" header.  Is that correct?

    Under the covers, HttpClient is using the HttpWebRequest, which doesn't allow you to set the Host header.  However, this shouldn't be a problem for you because it will set the Host header based on the host in URI of the request.  See http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx for more details.

    Do you have a need to set the host header to some value other than what is in the URI of the request?

    Thanks

  • Re: Error when setting "Host" HTTP Header

    10-15-2009, 1:19 AM
    • Member
      point Member
    • johnsheehan
    • Member since 10-13-2009, 3:05 PM
    • Posts 2

    Your assumptions are correct. My apologies for leaving out the detail. In the case of s3, all the requests are made to s3.amazonaws.com but you set which bucket you want to act on with the Host header, which for all bucket operations is different than the host used for the request. 

  • Re: Error when setting "Host" HTTP Header

    10-15-2009, 12:32 PM
    • Member
      90 point Member
    • randallt
    • Member since 02-11-2009, 11:53 AM
    • Posts 30

    I'm not an expert on S3, but looking at the documentation quickly I believe that you still include the bucket name in the request URI, and therefore, the host header would get set correctly by the HttpClient.

    Looking at the s3 documentation at: http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/index.html?UsingBucket.html.  It says:

    "The similarities between buckets and domain names is not a coincidence—there is a direct mapping between Amazon S3 buckets and subdomains of s3.amazonaws.com. Objects stored in Amazon S3 are addressable using the REST API under the domain bucketname.s3.amazonaws.com. For example, if the object homepage.html is stored in the Amazon S3 bucket mybucket its address would be http://mybucket.s3.amazonaws.com/homepage.html."

    Thanks

Page 1 of 1 (4 items)