【原創】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/"問題得到解決。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM