【原创】WebReuest在GetResponse()时异常信息为ProtocolError的解决

原文地址 http://www.cnblogs.com/zhoushenglong/archive/2012/07/19/2599241.html

下面是客户端获取回应的部分代码

          Uri uri = new Uri(requestUrl);
          WebRequest wReq = System.Net.WebRequest.Create(uri);
          // Get the response instance.
          WebResponse wResp = wReq.GetResponse();

服务端如下

            HttpListener listener = new HttpListener();
            listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证  Anonymous匿名访问
            listener.Prefixes.Add("http://localhost:9090/" );
            listener.Start();
            listener.BeginGetContext(OnClientConnect, listener);

客户端发送类似"http://127.0.0.1:9090/" 或"http://localhost:9090/"的url时正常返回,GetResponse()获得相应结果。问题在于若客户端和服务端在不同主机时,发送形如“http://服务端IP:9090/”的url在GetResponse()时获得了WebException,Status状态为ProtocolError。后来看了http://msdn.microsoft.com/zh-cn/library/system.net.httplistener.aspx的说明,将服务端监听的url设为"http://*:9090/"问题得到解决。

 

posted on 2012-07-19 14:44  枫江白露  阅读(4685)  评论(0编辑  收藏  举报

导航