// 待请求的地址
string url = "http://www.cnblogs.com"; // 创建 WebRequest 对象,WebRequest 是抽象类,定义了请求的规定,
// 可以用于各种请求,例如:Http, Ftp 等等。
// HttpWebRequest 是 WebRequest 的派生类,专门用于 Http
System.Net.HttpWebRequest request
= System.Net.HttpWebRequest.Create(url) as System.Net.HttpWebRequest; // 请求的方式通过 Method 属性设置 ,默认为 GET
// 可以将 Method 属性设置为任何 HTTP 1.1 协议谓词:GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。
request.Method = "POST"; // 还可以在请求中附带 Cookie
// 但是,必须首先创建 Cookie 容器
request.CookieContainer = new System.Net.CookieContainer(); System.Net.Cookie requestCookie
= new System.Net.Cookie("Request", "RequestValue","/", "localhost");
request.CookieContainer.Add(requestCookie); Console.WriteLine("请输入请求参数:"); // 输入 POST 的数据.
string inputData = Console.ReadLine(); // 拼接成请求参数串,并进行编码,成为字节
string postData = "firstone=" + inputData;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(postData); // 设置请求的参数形式
request.ContentType = "application/x-www-form-urlencoded"; // 设置请求参数的长度.
request.ContentLength = byte1.Length; // 取得发向服务器的流
System.IO.Stream newStream = request.GetRequestStream(); // 使用 POST 方法请求的时候,实际的参数通过请求的 Body 部分以流的形式传送
newStream.Write(byte1, 0, byte1.Length); // 完成后,关闭请求流.
newStream.Close(); // GetResponse 方法才真的发送请求,等待服务器返回
System.Net.HttpWebResponse response
= (System.Net.HttpWebResponse)request.GetResponse(); // 首先得到回应的头部,可以知道返回内容的长度或者类型
Console.WriteLine("Content length is {0}", response.ContentLength);
Console.WriteLine("Content type is {0}", response.ContentType); // 回应的 Cookie 在 Cookie 容器中
foreach (System.Net.Cookie cookie in response.Cookies)
{
Console.WriteLine("Name: {0}, Value: {1}", cookie.Name, cookie.Value);
}
Console.WriteLine(); // 然后可以得到以流的形式表示的回应内容
System.IO.Stream receiveStream
= response.GetResponseStream(); // 还可以将字节流包装为高级的字符流,以便于读取文本内容
// 需要注意编码
System.IO.StreamReader readStream
= new System.IO.StreamReader(receiveStream, Encoding.UTF8); Console.WriteLine("Response stream received.");
Console.WriteLine(readStream.ReadToEnd()); // 完成后要关闭字符流,字符流底层的字节流将会自动关闭
response.Close();
readStream.Close();

  使用WebRequest对象调用新浪天气预报

public string GetWeather(string city)
{
string weatherHtml = string.Empty;
//转换输入参数的编码类型
string cityInfo = HttpUtility.UrlEncode(city,System.Text.UnicodeEncoding.GetEncoding("GB2312"));
//初始化新的webRequst
HttpWebRequest weatherRequest = (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php?city="+cityInfo); HttpWebResponse weatherResponse = (HttpWebResponse)weatherRequest.GetResponse();
//从Internet资源返回数据流
Stream weatherStream = weatherResponse.GetResponseStream();
//读取数据流
StreamReader weatherStreamReader = new StreamReader(weatherStream,System.Text.Encoding.Default);
//读取数据
weatherHtml = weatherStreamReader.ReadToEnd();
weatherStreamReader.Close();
weatherStream.Close();
weatherResponse.Close();
//针对不同的网站查看html源文件
return weatherHtml;
}

  

WebRequest使用的更多相关文章

  1. 用WebRequest +HtmlAgilityPack 从外网抓取数据到本地

    相信大家对于WebRequest 并不陌生,我们在C#中发请求的方式,就是创建一个WebRequest .那么如果我们想发一个请求到外网,比如国内上不了的一些网站,那么该怎么做呢? 其实WebRequ ...

  2. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别

    WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...

  3. c#利用WebClient和WebRequest获取网页源代码的比较

    前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...

  4. WebRequest 访问 https

    参考代码: 1: [TestMethod] 2: public void TestHttps() 3: { 4: var req =(HttpWebRequest) System.Net.WebReq ...

  5. WebRequest 获取网页乱码

    问题:在用WebRequest获取网页源码时得到的源码是乱码. 原因:1,编码不对 解决办法:设置对应编码 WebRequest request = WebRequest.Create(Url);We ...

  6. WebClient与WebRequest差异

    WebRequst的使用 WebClient和HttpWebRequst是用来获取数据的2种方式,在我的这篇数据访问(2)中主要是讲的WebClient的使用,一般而言,WebClient更倾向于“按 ...

  7. C#、.NET网络请求总结(WebClient和WebRequest)

    1.关于WebClient第三方的封装,支持多文件上传等 using System; using System.Collections.Generic; using System.Text; usin ...

  8. 使用WebRequest 检测 手机号归属地。 C#通用 使用json 和可设定超时的WebClient

    首先建立jsonObject,当然你也可以使用xml解析,目前介绍一下我使用的方法. /******************************************************** ...

  9. C#: Create a WebRequest with HTTP Basic Authentication

    http://blog.csdn.net/huangyaoshifog/article/details/4470675 myReq = WebRequest.Create(url); string u ...

随机推荐

  1. Google Analytics统计代码GA.JS中文教程

    2010-12-06 11:07:08|  分类: java编程 |  标签:google  analytics  ga  js  代码  |举报|字号 订阅     Google Analytics ...

  2. PHP执行系统命令

    <?phpexec("ping www.baidu.com -n 1",$output,$status);var_dump($output);var_dump($status ...

  3. 转载:有关qsort的使用方法和注意事项

    七种qsort排序方法 <本文中排序都是采用的从小到大排序> 一.对int类型数组排序 int num[100]; Sample: int cmp ( const void *a , co ...

  4. 图解Java内存回收机制

    在Java中,它的内存管理包括两方面:内存分配(创建Java对象的时候)和内存回收,这两方面工作都是由JVM自动完成的,降低了Java程序员的学习难度,避免了像C/C++直接操作内存的危险.但是,也正 ...

  5. 【转】Spring 获取web根目录 (Spring线程获取web目录/路径/根目录,普通类获取web目录)

    不使用Spring,怎样能在Listener启动的Thread中获取web目录,还真不完全确定.其实我觉得实际代码也很简单.就是基于普通的listener,然后在listener中获取web目录并放到 ...

  6. 【转】CSRF攻击的应对之道

    CSRF 背景与介绍CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,它在 2007 年曾被列为互联网 20 大安全隐患之一.其他安全隐患,比如 ...

  7. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. Apache Spark源码走读之24 -- Sort-based Shuffle的设计与实现

    欢迎转载,转载请注明出处. 概要 Spark 1.1中对spark core的一个重大改进就是引入了sort-based shuffle处理机制,本文就该处理机制的实现进行初步的分析. Sort-ba ...

  9. SVN 分支管理

    平时在工作中使用 SVN 只是限于 commit,update 这样的操作,至多再 reslove 解决一下冲突,没有用过分支管理.开发过程中一般都是一个功能开发完成之后整体进行提交,而最近在项目中有 ...

  10. javaWeb中servlet开发(5)——WEB开发模式:Mode I与Mode II

    1.servlet开发 2.model I模式 客户端通过访问JSP,调用里面的javabean,而通过javabean调用处理数据库的操作,javabean中有专门处理数据库的操作,数据库主要以DA ...