private static HttpResponseMessage ConvertResponse(RTHttpResponseMessage rtResponse)
{
HttpResponseMessage response = new HttpResponseMessage((HttpStatusCode)rtResponse.StatusCode);
response.ReasonPhrase = rtResponse.ReasonPhrase;
// Version
if (rtResponse.Version == RTHttpVersion.Http11)
{
response.Version = HttpVersionInternal.Version11;
}
else if (rtResponse.Version == RTHttpVersion.Http10)
{
response.Version = HttpVersionInternal.Version10;
}
else if (rtResponse.Version == RTHttpVersion.Http20)
{
response.Version = HttpVersionInternal.Version20;
}
else
{
response.Version = new Version(0,0);
}
bool success;
// Headers
foreach (KeyValuePair<string, string> headerPair in rtResponse.Headers)
{
if (headerPair.Key.Equals(HttpKnownHeaderNames.SetCookie, StringComparison.OrdinalIgnoreCase))
{
// The Set-Cookie header always comes back with all of the cookies concatenated together.
// For example if the response contains the following:
// Set-Cookie A=1
// Set-Cookie B=2
// Then we will have a single header KeyValuePair of Key=Set-Cookie, Value=A=1, B=2.
// However clients expect these headers to be separated(i.e.
// httpResponseMessage.Headers.GetValues("Set-Cookie") should return two cookies not one
// concatenated together).
success = response.Headers.TryAddWithoutValidation(headerPair.Key, CookieHelper.GetCookiesFromHeader(headerPair.Value));
}
else
{
success = response.Headers.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
}
Debug.Assert(success);
}
// Content
if (rtResponse.Content != null)
{
var rtResponseStream = rtResponse.Content.ReadAsInputStreamAsync().AsTask().Result;
response.Content = new StreamContent(rtResponseStream.AsStreamForRead());
foreach (KeyValuePair<string, string> headerPair in rtResponse.Content.Headers)
{
success = response.Content.Headers.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
Debug.Assert(success);
}
}
return response;
}
}