2017-06-26 41 views
-7
 WebRequest request = WebRequest.Create(url); 
     WebRequest.DefaultWebProxy = null; 

     request.Proxy = null; 
     WebResponse response = request.GetResponse(); 
     Stream data = response.GetResponseStream(); 
     using (StreamReader sr = new StreamReader(data)) 
     { 
      html = sr.ReadToEnd(); 
     } 

上面的代碼無法讀取/下載下列網頁閱讀的網站或網頁: 1)https://en.wikipedia.org/wiki/Unified_Payments_Interface 2)http://www.npci.org.in/UPI_Background.aspx下面的C#代碼不是從給定的URL

+0

所以它有什麼作用?它以什麼方式得不到它,什麼是包含它何時「不」的響應變量 – BugFinder

+0

如果我知道,它以什麼方式得不到它,那我爲什麼要問。 –

+0

那麼你應該可以在getresponse之後去調試並查看響應變量...... – BugFinder

回答

2

這可能會幫助你下面的代碼包含用於獲取和發佈數據:

public static string PostContent(this Uri url, string body, string contentType = null) 
    { 
     var request = WebRequest.Create(url); 
     request.Method = "POST"; 

     if (!string.IsNullOrEmpty(contentType)) request.ContentType = contentType; 

     using (var requestStream = request.GetRequestStream()) 
     { 
      if (!string.IsNullOrEmpty(body)) using (var writer = new StreamWriter(requestStream)) { writer.Write(body); } 

      using (var response = request.GetResponse() as HttpWebResponse) 
      { 
       using (var stream = response.GetResponseStream()) 
       using (var reader = new StreamReader(stream)) 
       { 
        return reader.ReadToEnd(); 
       } 
      } 
     } 
    } 

    public static string GetContent(this Uri url) 
    { 
     WebClient client = new WebClient(); 
     try 
     { 
      using (client) 
      { 
       client.Encoding = Encoding.UTF8; 

       return client.DownloadString(url); 
      } 
     } 
     catch (WebException) 
     { 
      return ""; 
     } 
     finally 
     { 
      client.Dispose(); 
      client = null; 
     } 
    } 
+1

非常感謝... –

+0

它正在處理所有URL –

1

請注意.aspx文件擴展名設計服務器端頁面,因此您只能下載在這些網站上導航時顯示的HTML頁面(這與.php文件相同)。

,但如果你想下載的前端視圖這應該工作:

using System.Net; 
... 
WebClient client = new WebClient(); 
client.DownloadFile("Your url","download location");