2014-02-11 5 views
0

어플 리케이션으로 우리의 dyndns 영역을 업데이트해야합니다.http가있는 Dyndns 업데이터가

그들의 API는 enter link description here

에 위치해 그들은 내가 그래서 GET 요청 liek을해야 말 :

GET /nic/update? hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG HTTP/1.0 
Host: members.dyndns.org 
Authorization: Basic base-64-authorization 
User-Agent: Company - Device - Version Number 

어떻게 C#에서 이런 짓을 했을까?

나는이 시도 :

String request = "/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG HTTP/1.0"; 
WebRequest webRequest = WebRequest.Create(request); 
WebResponse webResp = webRequest.GetResponse(); 
Console.WriteLine(webResp.ToString() 

을하지만 호스트 이름과 모든 것을 할 어떻게?

+0

을, '어떻게 호스트 이름을합니까 '? –

+0

예에서 호스트 구성원은 dynds.org입니다. 내 요청 문자열에 내가 그나마있어? – Zapnologica

+0

아니요.하지만 다른 호스트 이름이 있습니다 (예 : –

답변

1

나는 eventully가 맞았던 코드를 게시하고 싶었고, 다른 사람은 도움이 필요할 수도 있습니다.

간단히하기 위해 하위 기능으로 나눴습니다. 그것이 당신을 놀라게하지 마십시오. 여기

/// <summary> 
/// Call this from another class to update a zone. 
/// </summary> 
/// <param name="host">The full name of the host</param> 
/// <returns></returns> 
public string Update(String host) 
{ 
    string url = BuildUrl(host, Ip); 
    return PerformUpdate(url); 
} 

업데이트 수행하는 함수 URL을 여기에

/// <summary> 
/// //Constructs the url to send the get request to. 
/// </summary> 
/// <param name="hostname">the hostname </param> 
/// <param name="ip">the ipaddress</param> 
/// <returns>The complete String</returns> 
private string BuildUrl(String hostname, String ip) 
{ 
    return BaseUrl + "hostname=" + hostname + "&myip=" + ip; 
} 

을 구축 할 수있는 기능입니다 : 당신이 의미하는 무엇을

/// <summary> 
/// Performs the actual request to the dyndns server to update the entity 
/// </summary> 
/// <param name="url">url to post</param> 
private String PerformUpdate(String url) 
{ 
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
    NetworkCredential creds = new NetworkCredential(Username, Password); 
    request.UserAgent = Username + " - " + Password + " - " + "0.01"; 
    request.Credentials = creds; 
    request.Method = "GET"; 
    HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
    Stream reply = response.GetResponseStream(); 
    StreamReader readReply = new StreamReader(reply); 
    return readReply.ReadToEnd(); 
} 
+0

with MyWebRequest content = new MyWebRequest ("http://checkip.dyndns.org/"); 또한 귀하의 IP를 얻을 수도 있습니다. 물론 당신은 콘텐츠를 IP로 잘라야합니다. – Dwza