2010-07-16 17 views
20

Cevabın hayır olduğunu kabul edeceğim .... WebClient'i HEAD yöntemini göndermek ve başlıkları bir dize veya benzeri bir şey olarak döndürmek için bir yol var mı?WebClient ile KAFA?

cevap

17

WebClient bunu desteklemiyor. HttpWebRequest kullanmak ve bu işlevselliği isterseniz HEAD yöntemini ayarlayabilirsiniz: Ben bu yöntemi kabul edecek talep

System.Net.WebRequest request = System.Net.WebRequest.Create(uri); 
request.Method = "HEAD"; 
request.GetResponse(); 
3

Birçok web sunucusu. Her web sunucusu olmasa da. IIS6, örneğin, SOMETIMES istek yöntemini onurlandırır.

Bu yöntem izin verilmez, döndürülen durum kodu ...

catch (WebException webException) 
      { 
        if (webException.Response != null) 
        { 
         //some webservers don't allow the HEAD method... 
         if (((HttpWebResponse) webException.Response).StatusCode == HttpStatusCode.MethodNotAllowed) 

sayesinde Mike

+0

Bu, WebClient'in bir HEAD isteği göndermesini destekleyip desteklemediği sorusunu yanıtlamaz. Webclient olarak –

14

başka yolu da WebClient'a devralan ve GetWebRequest(Uri address) geçersiz etmektir.

public class ExWebClient : WebClient 
    { 
     public string Method 
     { 
      get; 
      set; 
     } 

     protected override WebRequest GetWebRequest(Uri address) 
     { 
      WebRequest webRequest = base.GetWebRequest(address); 

      if (!string.IsNullOrEmpty(Method)) 
       webRequest.Method = Method; 

      return webRequest; 
     } 
    } 
+0

+1 kullanımı çok daha kolaydır. –

+1

Yeni başlayanlar için bu geçersiz kılmayı nasıl sınıflandırabilirim? – bendecko

+0

'var wc = yeni ExWebClient();' yerine var wc = yeni WebClient(); ' – tomfanning