首先,我是新的 http 和 mjpeg-stream,所以我很可能只有理解问题。也许有人可以在这里帮助我。
我从我的网络摄像机获取 mjpeg 流时遇到问题。调用流的链接在浏览器中工作正常,我可以观看直播。(例如http://192.168.xx.xx:8081/cgi-bin/hi3510/mjpegstream.cgi?-chn=11&-usr=user&-pwd=password,其中 xx.xx 和 user, password 是占位符)
如果我尝试在 uwp 应用程序中连接到 ip 摄像头,我将无法返回任何流。我尝试了很多解决方案,但没有一个仍然适合我。(这些是一些尝试的解决方案:
- Mjpeg处理器
- 大关相机 SDK
- 网络摄像机查看器
- Onvif Camera Viewer )我也在这里阅读了很多有用的文章,但现在有一个正确的答案。
我的源代码如下所示:
var newParams = new[]
{
new KeyValuePair<string, string>("-chn", "11")
};
var baseUrl = new Url("http://192.168.xx.xx:8081");
Debug.WriteLine(baseUrl);
using (var fc = new FlurlClient(baseUrl).EnableCookies())
{
// login on page
var loginResponse = await baseUrl
.WithClient(fc)
.PostUrlEncodedAsync(new
{
usr = "user",
pwd = "password"
});
//StatusCode 200
if (loginResponse.IsSuccessStatusCode)
{
try
{
// get mjepg stream from page
var streamResult = await baseUrl
.AppendPathSegments(new string[] { "cgi-bin", "hi3510", "mjpegstream.cgi" })
.SetQueryParams(newParams)
.WithClient(fc)
.GetStreamAsync();
var memNewStream = new MemoryStream();
await streamResult.CopyToAsync(memNewStream);
memNewStream.Position = 0;
mediaElement.SetSource(memNewStream.AsRandomAccessStream(), "multipart/alternative");
}
catch (FlurlHttpException ex)
{
Debug.WriteLine(ex);
}
}
}
我总是得到一个: Flurl.Http.FlurlHttpException: Request to http://192.168.xx.xx:8081/cgi-bin/hi3510/mjpegstream.cgi?-chn=11&-usr=user&-pwd=password failed with状态码 401(未授权)var streamResult = await baseUrl .AppendPathSegments(new string[] { "cgi-bin", "hi3510", "mjpegstream.cgi" }) .SetQueryParams(newParams).WithClient(fc).GetStreamAsync();
。
如果我将 baseURL 更改为var baseUrl = new Url("http://192.168.xx.xx:8081/cgi-bin/hi3510/mjpegstream.cgi?-chn=11&-usr=user&-pwd=password");
并尝试连接,我会得到相同的状态码 401。
提前谢谢了!
更新: 浏览器中调用流的链接htp://192.168.xx.xx:8081/cgi-bin/hi3510/mjpegstream.cgi?-chn=11&-usr=user&-pwd=password。 -> 好的
在上面的源代码示例中,第一次调用
var loginResponse = await baseUrl.WithClient(fc).PostUrlEncodedAsync(new { usr = "user", pwd = "password" });
-> OK Url http://192.168.xx.xx:8081
在上面的 Sourcode 示例中,第二次调用
var streamResult = await baseUrl.AppendPathSegments(new string[] { "cgi-bin", "hi3510", "mjpegstream.cgi" }).SetQueryParams(newParams).WithClient(fc).GetStreamAsync();
-> Exception Flurl.Http.FlurlHttpException: Request to http://192.168.178.35:8081/cgi-bin/hi3510/mjpegstream.cgi?-chn=11 failed with status code 401 (Unauthorized).
Url http://192.168.xx.xx:8081/cgi-bin/hi3510/mjpegstream.cgi?-chn=11
那正是你想要的?