2013-07-09 45 views
0

我要訪問一個網頁&商店的網頁內容到一個數據庫 內容,這是我曾嘗試讀取網頁如何讀取網頁

public static WebClient wClient = new WebClient(); 
    public static TextWriter textWriter; 
    public static String readFromLink() 
    { 
     string url = "http://www.ncedc.org/cgi-bin/catalog-search2.pl"; 
     HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest; 
     webRequest.Method = "POST"; 
     System.Net.WebClient client = new System.Net.WebClient(); 
     byte[] data = client.DownloadData(url); 
     string html = System.Text.Encoding.UTF8.GetString(data); 
     return html; 
    } 
    public static bool WriteTextFile(String fileName, String t) 
    { 

     try 
     { 
      textWriter = new StreamWriter(fileName); 
     } 
     catch (Exception) 
     { 
      return false; 
      Console.WriteLine("Data Save Unsuccessful: Could Not create File"); 
     } 

     try 
     { 
      textWriter.WriteLine(t); 
     } 
     catch (Exception) 
     { 
      return false; 
      Console.WriteLine("Data Save UnSuccessful: Could Not Save Data"); 
     } 
     textWriter.Close(); 
     return true; 
     Console.WriteLine("Data Save Successful"); 
    } 
    static void Main(string[] args) 
    { 
     String saveFile = "E:/test.txt"; 
     String reSultString = readFromLink(); 
     WriteTextFile(saveFile, reSultString); 
     Console.ReadKey(); 
    } 

但是這個代碼的內容的代碼給我一個O/p AS-This script should be referenced with a METHOD of POST. REQUEST_METHOD=GET

請告訴我如何解決這個

+1

你已經發明瞭一個24行方法來完成'File.WriteAllText'的功能。第一種方法的線條甚至不重要... –

回答

3

您正在混合HttpWebRequest與System.Net.WebClient代碼。他們是不同的。您可以使用WebClient.UploadValues通過WebClient發送POST。您還需要提供一些POST數據:

System.Net.WebClient client = new System.Net.WebClient(); 
    NameValueCollection postData = new NameValueCollection(); 
    postData.Add("format","ncread"); 
    postData.Add("mintime","2002/01/01,00:00:00"); 
    postData.Add("minmag","3.0"); 
    postData.Add("etype","E"); 
    postData.Add("outputloc","web"); 
    postData.Add("searchlimit","100000"); 
    byte[] data = client.UploadValues(url, "POST", postData); 
    string html = System.Text.Encoding.UTF8.GetString(data); 

你可以找出哪些參數在提琴手檢查POST消息傳遞。是的,正如@Chris Pitman所評論的那樣,使用File.WriteAllText(path, html);

+0

thanx :) bt我有一個更多的查詢...我應該直接POST到指定的網址(在代碼中提到),或者我必須POST到之前的頁面? (http://www.ncedc.org/anss/catalog-search.html) – Rannon

+0

您應該使用代碼中的那個,即POST URL。這個(ncedc.org/anss/catalog-search.html)是GET頁面,其中的值被編譯成表單併發送到POST頁面。你有問題得到POST迴應? – fcuesta

+0

thanx我得到了解決方案:)現在我所得到的內容都是混合的,即它位於html標籤之間,我如何只將所需內容及其存儲在任何文件中(例如CSV) – Rannon

0

我不知道這是否是在你的身邊出現故障,因爲我通過打開的頁面得到相同的消息而已。頁面源碼不包含任何html,所以我不認爲你可以做webRequest.Method =「POST」。您是否與該網站的管理員通話?

0

.NET框架提供了一套豐富的方法來訪問存儲在Web上的數據。首先,你必須包括正確的命名空間:

using System.Text; 
using System.Net; 
using System.IO; 

的HttpWebRequest對象允許我們創建的URL的請求,並提供WebResponse使我們能夠讀取到請求的響應。

我們將使用StreamReader對象將響應讀入字符串變量。

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL); 
myRequest.Method = "GET"; 
WebResponse myResponse = myRequest.GetResponse(); 
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8); 
string result = sr.ReadToEnd(); 
sr.Close(); 
myResponse.Close(); 

在此代碼示例中,URL變量應包含您想要獲取的URL,並且結果變量將包含網頁的內容。您也可以爲真實應用程序添加一些錯誤處理。

+0

這是很好的信息和所有,但它很明顯,問題是他在做GET而不是POST來獲得一些搜索結果,而你的答案沒有任何幫助那。 –

0

據我所知,您請求的URL是一個perl腳本。我認爲它需要POST來獲取搜索參數,並因此提供搜索結果。