MC Fer MC Fer - 1 year ago 188
C# Question

Error receiving a HttpResponseMessage OK

I am with a problem. I have 2 WebApi´s . The webapi2 get the data from DB and return the IMAGE. Here, its ok and working. If i try on browser, show me the image or if i change, the byte array.
The problem is with the Webapi1 that calls this webapi2. I always receive the HttpResponseMessage with false for IsSuccessStatusCode. The error is 500 internal server error.
I am a newbie and i don´t know what to do...i already tryed a lot of things

public async Task<HttpResponseMessage> GetFoto(string exemplo, string exemple2)
HttpClientHandler handler = new HttpClientHandler()
UseDefaultCredentials = true,
using (var client = new HttpClient(handler))

client.BaseAddress = new Uri("");

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage response = await client.GetAsync("api/Tr/Test?exemplo="+exemplo+"&pk="+pk+"");

if (response.IsSuccessStatusCode)
var data = response.Content.ReadAsByteArrayAsync().Result;
var stream = new MemoryStream(data);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

return response;

Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
return null;


My webapi that Works and return me a Image:

//connections code that doesn´t matter....


HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(imgBytes);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");

return result;


catch (Exception ex)
return Request.CreateResponse(HttpStatusCode.Gone);

Answer Source

The error is because when you pass a value on the browser, they change some carachters... So, you are passing 2 values... you have to use

On the webapi 1

var MIRACLE =  Uri.EscapeDataString(exemplo);

And at the webapi2

var MIRACLE2 = Uri.UnescapeDataString(MIRACLE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download