简体   繁体   English

从asp.net代码获取一些网站的来源

[英]Get the source of some website from asp.net code

Is there any way that I could get the source of a website (as a string preferably), let's say www.google.com, from some c# code inside code behind of asp.net website? 有没有什么方法可以获得一个网站的来源(最好是一个字符串),让我们说www.google.com,来自asp.net网站背后代码中的一些c#代码?

edit: of course i mean html code - in every browser you can view it using "view source " in context menu. 编辑:我当然是指html代码 - 在每个浏览器中,你可以使用上下文菜单中的“查看源代码 ”查看它。

Assuming you want to retrieve the html: 假设您要检索html:

class Program
{
    static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        using (Stream stream = client.OpenRead("http://www.google.com"))
        using (StreamReader reader = new StreamReader(stream))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}

For C#, I prefer to use HttpWebRequest over WebClient because you can have more option in the future like having GET/POST parameter, using Cookies, etc. 对于C#,我更喜欢在WebClient上使用HttpWebRequest ,因为您可以在将来拥有更多选项,例如使用GET / POST参数,使用Cookie等。

You can have a shortest explication at MSDN . 您可以在MSDN上进行最短的解释。

Here is the example from MSDN: 以下是MSDN的示例:

        // Create a new HttpWebRequest object.
        HttpWebRequest request=(HttpWebRequest) WebRequest.Create("http://www.contoso.com/example.aspx");    

        // Set the ContentType property. 
        request.ContentType="application/x-www-form-urlencoded";
        // Set the Method property to 'POST' to post data to the URI.
        request.Method = "POST";
        // Start the asynchronous operation.    
        request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);    

        // Keep the main thread from continuing while the asynchronous
        // operation completes. A real world application
        // could do something useful such as updating its user interface. 
        allDone.WaitOne();

        // Get the response.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        Console.WriteLine(responseString);
        // Close the stream object.
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse.
        response.Close();

这不是最明显(也是最好)的方式,但我发现在Windows窗体中你可以使用WebBrowser控件(如果你确实需要它),用你需要的url填充它的Url属性,当它加载时,读取DocumentText属性 - 它包含所查看网站的html代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务器获取我发布的ASP.NET网站的源代码 - Get source code of my published ASP.NET website from server 保护ASP.NET网站的源代码 - Protect source code of an ASP.NET website 如何使用ASP.NET MVC每天从另一个网页获取“content = source code”? - How to Use ASP.NET MVC to get “content=source code” from another web page once a day? 在asp.net中自动完成jquery,来自代码背后的源代码 - Autocomplete jquery in asp.net, source from code behind 从 ASP.NET 网站获取 IIS 站点名称 - Get IIS site name from for an ASP.NET website 如果没有源代码/项目解决方案,则将页面添加到asp.net网站 - Add page to asp.net website if source code/project solution is not available 在 asp.net 网站上,每个登录用户是否都有自己正在运行的源代码的实例“副本”? - On an asp.net Website, does each logged in user have their own instance “copy” of the source code being run? 如何在我的网站上实现asp.net的任何源代码以获得教程目的 - How to implement any source code of asp.net in my website for tutorial Purpose 我可以重新创建ASP.NET网站的源代码和解决方案文件吗? - Can I recreate the source code and solution files for an ASP.NET website? 更新ASP.NET网站后端代码 - Updating an ASP.NET website backend code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM