简体   繁体   中英

API call using Auth to get image using HttpClient not working

I'm attempting to retrieve an image with an API call using HttpClient in ASP.Net Core. The problem is, it works with regular images (ones that include the file extension type), but not with my URL.

I don't understand if this has something to do with the API URL involving credentials or not.

Here's a call that works:

HttpResponseMessage response = await client.GetAsync(
        "https://i.stack.imgur.com/hM8Ah.jpg?s=48&g=1");
byte[] content = await response.Content.ReadAsByteArrayAsync();
return "data:image/png;base64," + Convert.ToBase64String(content);

And here's mine that doesn't:

HttpResponseMessage response = await client.GetAsync(
        "http://user:pass@CAMERA-IP/cgi-bin/snapshot.cgi?channel=0&authbasic=aXU8Hu1");
byte[] content = await response.Content.ReadAsByteArrayAsync();
return "data:image/png;base64," + Convert.ToBase64String(content);

Has anyone ever encountered this problem?

Anyone know why this isn't working?


Here's more info about that API: http://www.techprosecurity.com/

It's a camera system that's hooked up to a location's IP Address that requires a user/pass to access each camera (channel).

I found a workaround by simply embedding the authorization inside the headers:

HttpClient client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

HttpResponseMessage response = await client.GetAsync("http://CAMERA-IP/cgi-bin/snapshot.cgi?channel=0");
byte[] myBytes = await response.Content.ReadAsByteArrayAsync();
string convertedFromString = Convert.ToBase64String(myBytes);

return "data:image/png;base64," + convertedFromString;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM