原文地址 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/"問題得到解決。
