背景:用winfrom做一个小的http服务器,放在云服务器进行测试;
问题:在本地运行正常,放到服务器报异常,异常信息如下:
详细异常文本:
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。
************** 异常文本 **************
System.Net.HttpListenerException (0x80004005): 指定的网络名格式无效。
在 System.Net.HttpListener.AddAllPrefixes()
在 System.Net.HttpListener.Start()
在 app服务器通讯测试.Form1.button1_Click(Object sender, EventArgs e) 位置 d:\用户目录\我的文档\Visual Studio 2013\Projects\app服务器通讯测试\app服务器通讯测试\Form1.cs:行号 60
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
异常代码:
httplistener = new HttpListener();
httplistener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 :匿名
httplistener.Prefixes.Add(requestUrl);
httplistener.Start(); //异常在此触发;
解决思路:
- 查看端口是否开启
- 查看网络连接时否ping通
- 在本地测试查看有无异常;
- 通过添加 URL 保留项
cmd命令: netsh http add urlacl url=http://*:端口号/ user=Everyone
实际解决办法:
将url写死到程序里; 可能出现问题原因,外部输入字符串拼接问题;