2017-07-12 29 views
0

我試圖在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地址的攝像系統。

+0

你能告訴我們多一點點有關圖像API? – Win

+0

@Win我繼續編輯我的問題。 – NoReceipt4Panda

+0

你能通過瀏覽器獲取該圖像嗎?你確定這個url有'http'方案嗎? –

回答

0

我找到了一個解決方法通過簡單地嵌入頭部內的授權:

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;