2013-02-11 178 views
0

我正在嘗試爲我的聊天程序創建一個通用下落定位器。它目前顯示IP,用戶名,我正在嘗試添加位置。我試圖使用這段代碼:(403)WebRequest禁止

var location = ""; 
List<string> HTML_code = new List<string>(); 
WebRequest request = WebRequest.Create("http://www.maxmind.com/app/locate_demo_ip?ips=" + IP); 
using (WebResponse response = request.GetResponse()) 
using (StreamReader stream = new StreamReader(response.GetResponseStream())) 
{ 
    string line; 
    while ((line = stream.ReadLine()) != null) 
    { 
    HTML_code.Add(line); 
    } 
} 

location = (HTML_code[296].Replace("<td><font size=\"-1\">", "")).Replace("</font></td>", ""); 
return location; 

但這返回有關WebResponse的異常:

The remote server returned an error: (403) Forbidden.(System.Net.WebResponse GetResponse())

爲什麼會出現呢?我該如何預防它?

回答

2

你必須有許可證密鑰才能使用這項服務,書面here

All of the services take the same parameters as inputs. The only difference between them is the URI they use and the data they return. The two parameters that each service takes are the IP address to look up and your MaxMind license key.

一旦你得到這樣一個關鍵,你必須將它添加到URL除了IP地址。

+0

我明白了,非常感謝 – Daaksin 2013-02-11 10:02:54

0

我找不到該特定REST API調用的文檔,但公司網站明確表示此服務不是免費的(至少不是沒有許可證密鑰)。這是一種經典的方法(您可以使用您的應用程序/許可證密鑰隨API發送每個請求),例如Twitter和Facebook都擁有它。

看看this link,他們給出了一個如何使用C#中的GeoIP服務的代碼示例。另外,請考慮看看他們的free databases