For a project I need to build a "proxy" for an third party API because parts of the API should be blocked for an external party. Now I can use HttpClient to retrieve the information from the API but I need to send the information back using HttpResponse. For a lot I can send information back but sometimes the information is send back as gzip or binary. What I'm trying to do is copying all Headers from HttpResponseMessage to HttpResponse but I fail miserably. Does anyone done something like this?
public static void AddHeaders(HttpResponse httpResponse, System.Net.Http.HttpResponseMessage httpResponseMessage)
{
httpResponse.CacheControl = httpResponseMessage.Headers.CacheControl.ToString();
foreach (KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> item in httpResponseMessage.Headers)
{
AddHeaders(httpResponse, item);
}
foreach (KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> item in httpResponseMessage.Content.Headers)
{
AddHeaders(httpResponse, item);
}
}
public static void AddHeaders(HttpResponse httpResponse, KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> keyValuePair)
{
string key = keyValuePair.Key;
string values = "";
foreach (string stringValue in keyValuePair.Value)
{
values += "," + stringValue;
values = values.Trim(',');
}
switch (key)
{
case "Content-Type":
httpResponse.ContentType = values;
break;
default:
httpResponse.Headers.Add(key, values);
break;
}
}