I need help retrieving a picture from a web service. It's a GET request.
At the moment I am retrieving a response and I am able to convert it to a byte array, which is what I am going for, but the byte array crashes the app, so I don't think the content is set right.
I have tried to set the response content with:
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = filename };
Even though my guess is that it is set incorrectly, or it is up in the requestHeader it is set wrong.
Any ideas?
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseurl.uri);
client.DefaultRequestHeaders.Add("x-access-token", sessionToken);
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("image/jpeg")
);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "");
try
{
Task<HttpResponseMessage> getResponse = client.SendAsync(request);
HttpResponseMessage response = new HttpResponseMessage();
response = await getResponse;
//1. response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = filename };
//2. response.Content.Headers.ContentType =
//new MediaTypeHeaderValue("image/jpeg");//("application/octet-stream");
byte[] mybytearray = null;
if (response.IsSuccessStatusCode)
{
//3.
mybytearray = response.Content.ReadAsByteArrayAsync().Result;
}
var responseJsonString = await response.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine(responseJsonString);
System.Diagnostics.Debug.WriteLine("GetReportImage ReponseCode: " + response.StatusCode);
return mybytearray;//{byte[5893197]}
}
catch (Exception ex)
{
string message = ex.Message;
return null;
}
}