使用Auth获取API调用使用HttpClient不工作的图像
问题描述:
我试图在ASP.Net Core中使用HttpClient
调用API调用图像。问题是,它适用于常规图像(包含文件扩展名类型),但不适合我的网址。使用Auth获取API调用使用HttpClient不工作的图像
我不明白这是否与涉及凭证的API URL有关。
这里是一个可行的呼叫:
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);
这里是我认为不会:
HttpResponseMessage response = await client.GetAsync(
"http://user:[email protected]/cgi-bin/snapshot.cgi?channel=0&authbasic=aXU8Hu1");
byte[] content = await response.Content.ReadAsByteArrayAsync();
return "data:image/png;base64," + Convert.ToBase64String(content);
有没有人遇到过这个问题吗?
任何人都知道为什么这不起作用?
下面是关于API的更多信息:http://www.techprosecurity.com/
这是一个需要用户名/密码来访问每个摄像机(频道)这是挂接到一个位置的IP地址的摄像系统。
答
我找到了一个解决方法通过简单地嵌入头部内的授权:
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;
你能告诉我们多一点点有关图像API? – Win
@Win我继续编辑我的问题。 – NoReceipt4Panda
你能通过浏览器获取该图像吗?你确定这个url有'http'方案吗? –