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

请告诉我如何解决这个问题

4

4 回答 4

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);

您可以通过检查 Fiddler 中的 POST 消息来找出要传递的参数。是的,正如@Chris Pitman 评论的那样,使用File.WriteAllText(path, html);

于 2013-07-09T04:45:34.757 回答
0

我不确定这是否是您的错误,因为我只是打开页面就收到了相同的消息。页面源不包含任何 html,所以我认为您不能执行 webRequest.Method = "POST"。你和网站管理员谈过吗?

于 2013-07-09T04:21:58.130 回答
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,结果变量将包含网页的内容。您可能还想为实际应用程序添加一些错误处理。

于 2013-07-09T04:28:47.363 回答
0

据我所知,您请求的 URL 是一个 perl 脚本。我认为它需要 POST 来获取搜索参数并因此提供搜索结果。

于 2013-07-09T04:29:30.683 回答