// 待请求的地址
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;
}

最新文章

  1. 【RoR win32】提高rails new时bundle install运行速度
  2. panel面板
  3. Ubuntu 12.04下虚拟磁带库mhvtl的安装和使用
  4. Windows API 之 VirtualAlloc
  5. mongodb更新数据
  6. H5本地储存Web Storage
  7. 如何修改linux开机运行配置脚本
  8. centos7 五大查找常用命令
  9. PHP 入门学习教程及进阶(源于知乎网友的智慧)
  10. Linux下配置环境变量—— .bashrc 和 /etc/profile
  11. python mysql and ORM
  12. 《面向对象程序设计(Java)》第四周学习总结
  13. 高并发负载均衡——nginx与lvs
  14. python 常见报错汇总
  15. ssh和scp时指定端口
  16. LA3485 Bridge
  17. P4186 【[USACO18JAN]Cow at Large G】
  18. virtualenv之python虚拟环境
  19. 多线程编程(三)--创建线程之Thread VS Runnable
  20. Swift 学习笔记 (继承)

热门文章

  1. [笔记]New in Chrome 66
  2. Immunity Debugger学习
  3. 【python小练】0013
  4. 什么是IO多路复用
  5. webpack学习笔记——解决多次输出的问题&自动编译之启用观察者模式,热重载
  6. 通过Cookie统计上次网页访问时间
  7. Vue.js 技术揭秘(学习) slot
  8. 网易云课堂《JS原创视频教程-知识点类》
  9. Javascript - DOM文档对象模型
  10. list补充,append()、extend()、insert()、remove()、del()、pop()、分片