当前位置: 首页>>代码示例>>C#>>正文


C# HttpWebRequest.GetResponse方法代码示例

本文整理汇总了C#中System.Net.HttpWebRequest.GetResponse方法的典型用法代码示例。如果您正苦于以下问题:C# HttpWebRequest.GetResponse方法的具体用法?C# HttpWebRequest.GetResponse怎么用?C# HttpWebRequest.GetResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了HttpWebRequest.GetResponse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Net;
using System.Text;
using System.IO;

    public class Test
    {
        // Specify the URL to receive the request.
        public static void Main (string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

            Console.WriteLine ("Content length is {0}", response.ContentLength);
            Console.WriteLine ("Content type is {0}", response.ContentType);
             
            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream ();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

            Console.WriteLine ("Response stream received.");
            Console.WriteLine (readStream.ReadToEnd ());
            response.Close ();
            readStream.Close ();
        }
    }

/*
The output from this example will vary depending on the value passed into Main 
but will be similar to the following:

Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>

*/
开发者ID:.NET开发者,项目名称:System.Net,代码行数:48,代码来源:HttpWebRequest.GetResponse

示例2: HttpWebRequest.GetResponse()

//引入命名空间
using System;
using System.Net;
using System.IO;

public class WebApp {
    public static void Main() {
        String page = "http://www.yoursite.net/index.html";
        HttpWebRequest site = (HttpWebRequest)WebRequest.Create(page);
        HttpWebResponse response =(HttpWebResponse)site.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader read = new StreamReader(dataStream);
        String data = read.ReadToEnd();
        Console.WriteLine(data);
    }
}
开发者ID:C#程序员,项目名称:System.Net,代码行数:16,代码来源:HttpWebRequest.GetResponse


注:本文中的System.Net.HttpWebRequest.GetResponse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。