Get the source of some website from asp.net code

前端 未结 3 1388
感情败类
感情败类 2020-12-30 17:38

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?

3条回答
  •  借酒劲吻你
    2020-12-30 17:54

    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.

    You can have a shortest explication at MSDN.

    Here is the example from 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();
    

提交回复
热议问题