C# 4.0 IP摄像机401未授权错误
在将IP摄像头与C#app连接时,我在一个rge/Samples/player中找到了一个简单的播放器。它只需要修改ip字符串,所以我添加了我的管理员:admin@192.168.1.239:81/videostream.cgi?速率=11以获得MJPEG视频流。编译时,我得到的错误是 远程服务器返回了一个未经授权的错误(401)。 Andre Kirillow在MJPEGstream.cs文件中提到 一些摄像机产生HTTP头,这不完全符合标准,这导致了.NET异常。为避免此异常,应设置httpWebRequest的useUnsafeHeaderParsing配置选项,以及使用应用程序配置文件可以执行的操作C# 4.0 IP摄像机401未授权错误,c#-4.0,C# 4.0,在将IP摄像头与C#app连接时,我在一个rge/Samples/player中找到了一个简单的播放器。它只需要修改ip字符串,所以我添加了我的管理员:admin@192.168.1.239:81/videostream.cgi?速率=11以获得MJPEG视频流。编译时,我得到的错误是 远程服务器返回了一个未经授权的错误(401)。 Andre Kirillow在MJPEGstream.cs文件中提到 一些摄像机产生HTTP头,这不完全符合标准,这导致了.NET异常。为避免此异常,应设置http
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>
好的,有两种方法可以做到这一点。我用上面的代码添加了.config文件,并对其进行了编程,但相同的错误仍然存在。播放器程序编码为
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Reflection;
using System.Net.Configuration;
using System.Net;
namespace Player
{
public partial class MainForm : Form
{
private Stopwatch stopWatch = null;
// Class constructor
public MainForm( )
{
InitializeComponent( );
}
private void MainForm_FormClosing( object sender, FormClosingEventArgs e )
{
CloseCurrentVideoSource( );
}
// "Exit" menu item clicked
private void exitToolStripMenuItem_Click( object sender, EventArgs e )
{
this.Close( );
}
// Open local video capture device
private void localVideoCaptureDeviceToolStripMenuItem_Click( object sender, EventArgs e )
{
VideoCaptureDeviceForm form = new VideoCaptureDeviceForm( );
if ( form.ShowDialog( this ) == DialogResult.OK )
{
// create video source
VideoCaptureDevice videoSource = form.VideoDevice;
// open it
OpenVideoSource( videoSource );
}
}
// Open video file using DirectShow
private void openVideofileusingDirectShowToolStripMenuItem_Click( object sender, EventArgs e )
{
if ( openFileDialog.ShowDialog( ) == DialogResult.OK )
{
// create video source
FileVideoSource fileSource = new FileVideoSource( openFileDialog.FileName );
// open it
OpenVideoSource( fileSource );
}
}
// Open JPEG URL
private void openJPEGURLToolStripMenuItem_Click( object sender, EventArgs e )
{
URLForm form = new URLForm( );
form.Description = "Enter URL of an updating JPEG from a web camera:";
form.URLs = new string[]
{
"http://195.243.185.195/axis-cgi/jpg/image.cgi?camera=1",
};
if ( form.ShowDialog( this ) == DialogResult.OK )
{
// create video source
JPEGStream jpegSource = new JPEGStream( form.URL );
// open it
OpenVideoSource( jpegSource );
}
}
// Open MJPEG URL
private void openMJPEGURLToolStripMenuItem_Click( object sender, EventArgs e )
{
URLForm form = new URLForm( );
form.Description = "Enter URL of an MJPEG video stream:";
form.URLs = new string[]
{
"http://admin@192.168.1.239:81/videostream.cgi?rate=11",
"mjpegSource by mobby"
};
if ( form.ShowDialog( this ) == DialogResult.OK )
{
// create video source
MJPEGStream mjpegSource = new MJPEGStream( form.URL );
// open it
OpenVideoSource( mjpegSource );
}
}
// Open video source
private void OpenVideoSource( IVideoSource source )
{
// set busy cursor
this.Cursor = Cursors.WaitCursor;
// stop current video source
//CloseCurrentVideoSource( );
// start new video source
videoSourcePlayer.VideoSource = source;
videoSourcePlayer.Start( );
// reset stop watch
stopWatch = null;
// start timer
timer.Start( );
this.Cursor = Cursors.Default;
}
// Close video source if it is running
private void CloseCurrentVideoSource( )
{
if ( videoSourcePlayer.VideoSource != null )
{
videoSourcePlayer.SignalToStop( );
// wait ~ 3 seconds
for ( int i = 0; i < 30; i++ )
{
if ( !videoSourcePlayer.IsRunning )
break;
System.Threading.Thread.Sleep( 100 );
}
if ( videoSourcePlayer.IsRunning )
{
videoSourcePlayer.Stop( );
}
videoSourcePlayer.VideoSource = null;
}
}
// New frame received by the player
private void videoSourcePlayer_NewFrame( object sender, ref Bitmap image )
{
DateTime now = DateTime.Now;
Graphics g = Graphics.FromImage( image );
// paint current time
SolidBrush brush = new SolidBrush( Color.Red );
g.DrawString( now.ToString( ), this.Font, brush, new PointF( 5, 5 ) );
brush.Dispose( );
g.Dispose( );
}
// On timer event - gather statistics
private void timer_Tick( object sender, EventArgs e )
{
IVideoSource videoSource = videoSourcePlayer.VideoSource;
if ( videoSource != null )
{
// get number of frames since the last timer tick
int framesReceived = videoSource.FramesReceived;
if ( stopWatch == null )
{
stopWatch = new Stopwatch( );
stopWatch.Start( );
}
else
{
stopWatch.Stop( );
float fps = 1000.0f * framesReceived / stopWatch.ElapsedMilliseconds;
fpsLabel.Text = fps.ToString( "F2" ) + " fps";
stopWatch.Reset( );
stopWatch.Start( );
}
}
}
public static bool SetAllowUnsafeHeaderParsing20()
{
//Get the assembly that contains the internal class
Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
if (aNetAssembly != null)
{
//Use the assembly in order to get the internal type for the internal class
Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
if (aSettingsType != null)
{
//Use the internal static property to get an instance of the internal settings class.
//If the static instance isn't created allready the property will create it for us.
object anInstance = aSettingsType.InvokeMember("Section",
BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });
if (anInstance != null)
{
//Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
if (aUseUnsafeHeaderParsing != null)
{
aUseUnsafeHeaderParsing.SetValue(anInstance, true);
return true;
}
}
}
}
return false;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
使用系统诊断;
使用大屏幕视频;
使用forge.Video.DirectShow;
运用系统反思;
使用System.Net.Configuration;
Net系统;
命名空间播放器
{
公共部分类主窗体:窗体
{
私人秒表秒表=null;
//类构造函数
公共主窗体()
{
初始化组件();
}
私有void主表单\表单关闭(对象发送方,表单关闭目标)
{
CloseCurrentVideoSource();
}
//单击“退出”菜单项
private void exitToolStripMenuItem\u单击(对象发送者,事件参数e)
{
这个.Close();
}
//开放式本地视频捕获设备
私有void localVideoCaptureDeviceToolStripMenuItem\u单击(对象发送方,事件参数e)
{
VideoCaptureDeviceForm=新的VideoCaptureDeviceForm();
if(form.ShowDialog(this)=DialogResult.OK)
{
//创建视频源
VideoCaptureDevice videoSource=form.VideoDevice;
//打开它
开放视频源(videoSource);
}
}
//使用DirectShow打开视频文件
私有void openVideofileusingDirectShowToolStripMenuItem\u单击(对象发送方,事件参数e)
{
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
//创建视频源
FileVideoSource fileSource=新的FileVideoSource(openFileDialog.FileName);
//打开它
开放视频源(文件源);
}
}
//打开JPEG URL
私有void openJPEGURLToolStripMenuItem\u单击(对象发送方,事件参数e)
{
URLForm=新的URLForm();
form.Description=“输入从网络摄像机更新JPEG的URL:”;
form.url=新字符串[]
{
"http://195.243.185.195/axis-cgi/jpg/image.cgi?camera=1",
};
if(form.ShowDialog(this)=DialogResult.OK)
{
//创建视频源
JPEGStream jpegSource=新的JPEGStream(form.URL);
//打开它
OpenVideoSource(jpegSource);
}
}
//打开MJPEG URL
私有void openMJPEGURLToolStripMenuItem\u单击(对象发送方,事件参数e)
{
URLForm=新的URLForm();
form.Description=“输入MJPEG视频流的URL:”;
form.url=新字符串[]
{
"http://admin@192.168.1.239:81/videostream.cgi?速率=11“,
“mobby提供的信息来源”
};
if(form.ShowDialog(this)=DialogResult.OK)
{
//创建视频源
MJPEGStream mjpegSource=新的MJPEGStream(form.URL);
//打开它
开放视频源;
}
}
//开放视频源
私有void OpenVideoSource(IVideoSource)
{
//设置忙光标
this.Cursor=Cursors.WaitCursor;
//停止当前视频源
//CloseCurrentVideoSource();
//启动新的视频源
videoSourcePlayer.VideoSource=源;
videoSourcePlayer.Start();
//重置秒表
秒表=零;
//启动计时器
timer.Start();
this.Cursor=Cursors.Default;
}
//关闭正在运行的视频源
私有void CloseCurrentVideoSource()
{
if(videoSourcePlayer.VideoSource!=null)
{
videoSourcePlayer.SignalToStop();
//等待~3秒
对于(int i=0;i<30;i++)
{
如果(!videoSourcePlayer.IsRunning)
打破
系统线程线程睡眠(100);
}
if(videoSourcePlayer.IsRunning)
{
videoSourcePlayer.Stop();
}
videoSourcePlayer.VideoSource=null;
}
}
//播放机接收到的新帧
私有void videoSourcePlayer_NewFrame(对象发送器,参考位图图像)
{
DateTime now=DateTime.now;
Graphics g=Graphics.FromImage(图像);
//绘制当前时间
SolidBrush笔刷=新的SolidBrush(颜色为红色);
g、 DrawString(现在是.ToString(),this.Font,brush,newpointf(5,5));
brush.Dispose();
g、 处置();
}
//关于计时器事件-收集统计信息
私有无效计时器(对象发送方、事件参数)
{
IVideoSource videoSource=videoSourcePlayer.videoSource;
如果(视频源!=null)
{
//获取自上次计时器计时以来的帧数
int framesReceived=videoSource.framesReceived;
如果(秒表==null)
{
秒表=新秒表();
stopWatch.Start();
}
其他的
{
stopWatch.Stop();
浮动fps=1000.0f*帧接收/sto
jpegSource.Login = "your username";
jpegSource.Password = "your password";
mjpegSource.Login = "your username";
mjpegSource.Password = "your password";