本文整理汇总了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>
*/
示例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);
}
}