首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何阅读网页内容

如何阅读网页内容
EN

Stack Overflow用户
提问于 2013-07-09 12:10:53
回答 4查看 6.4K关注 0票数 0

我想访问网页并将网页内容存储到数据库中,这是我尝试读取网页内容的代码

代码语言:javascript
复制
    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

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-07-09 12:45:35

您正在将HttpWebRequest与System.Net.WebClient代码混合在一起。他们是不同的。您可以通过WebClient使用WebClient.UploadValues发送帖子。您还需要提供一些POST数据:

代码语言:javascript
复制
    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消息来找出要传递的参数。是的,正如@克里斯·皮特曼所评论的那样,使用File.WriteAllText(path, html);

票数 3
EN

Stack Overflow用户

发布于 2013-07-09 12:21:58

我不确定这是不是你的错,因为我只是打开页面就得到了同样的信息。页面源代码不包含任何html,所以我不认为您可以执行webRequest.Method = "POST“。你和网站的管理员谈过了吗?

票数 0
EN

Stack Overflow用户

发布于 2013-07-09 12:28:47

.NET框架提供了一组丰富的方法来访问存储在web上的数据。首先,您必须包含正确的名称空间:

代码语言:javascript
复制
using System.Text; 
using System.Net;
using System.IO;

HttpWebRequest对象允许我们创建对该地址的请求,而WebResponse允许我们读取对该请求的响应。

我们将使用StreamReader对象将响应读取到字符串变量中。

代码语言:javascript
复制
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,而result变量将包含网页的内容。您可能还想为实际的应用程序添加一些错误处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17539756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档