WebRequest使用
// 待请求的地址 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"));//初始化新的webRequstHttpWebRequest 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;}
转载于:https://www.cnblogs.com/grayworm/p/4865681.html
WebRequest使用相关推荐
- php gzipstream,c# – 在WebRequest中发送gzip数据?
我有大量的数据(〜100k),我的C#应用程序发送到我的Apache服务器,安装了mod_gzip.我试图使用System.IO.Compression.GZipStream gzip数据. PH ...
- 利用WebClient和WebRequest类获得网页源代码C#
作者:不详 请速与本人联系 GetPageHtml.aspx <%@ Page language="c#" validateRequest = "fal ...
- SilverLight学习笔记--Silverlight中WebRequest通讯
本文我们学习如何使用WebRequest类实现客户端和服务器端的通讯. 本例处理过程:在客户端,我们在文本框中输入任意文本,然后用POST方法向服务器端传递信息,服务器端收到从客户端传来的信 ...
- c# webrequest 自动登入网站
这两天用c# 写了一个自动登入网页的小工具.使用的是发送webrequest的方式. 首先,使用抓包工具fiddler抓取登入页面时发送的request. 通过抓包工具,能够获取登入页面时发送的请求, ...
- C# WebRequest 基础连接已关闭 连接意外关闭
1. 出现这种问题一般是因为request和response没有释放造成的,在请求处理完毕对请求request调用abort,对response调用close处理一下就好了. 2.我自己试的是这种: ...
- c#利用WebClient和WebRequest获取网页源代码的比较
前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...
- 一步一步学Silverlight 2系列(13):数据与通信之WebRequest
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- 【错误记录】Flutter 报错 Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...
文章目录 一. 报错信息 二.解决方案 ( 检查镜像地址环境变量设置 ) 1.检查镜像地址环境变量设置 ( 错误方案 没有解决问题 ) 2.尝试使用官方地址 三.总结 1.官方镜像 ( 推荐方案 ) ...
- webclient 和 webrequest获取网页源码的
private void WebClientButton_Click(object sender, System.EventArgs e) { PageUrl = UrlText.Text; WebC ...
- httpWebRequest和webRequest的区别
httpWebRequest是webRequest的子类,httpWebRequest是基于http协议的 . HttpWebRequest 是 WebRequest 的实例化使用,单独的 WebRe ...
最新文章
- 03Template Method模式
- 人工智能中常见的误区
- 实验3 linux内存映射,3Linux内存映射,mmap()函数
- 安卓逆向_12 --- jeb工具的使用 ( 动态调试 smali 代码 【 普通调试 和 debug调试 】)
- mysql忘记密码怎么办?
- i/o timeout , 希望你不要踩到这个net/http包的坑
- 类和对象编程(七):this指针
- SQL项目实战练习:淘宝用户行为数据分析实战
- Jetbrains IDE 各种 plugins 插件下载
- Python 字符串重复判断
- jmeter实现手机号归属地接口测试案例
- 转回到Edison上开发
- Hadoop Namenode元数据文件 Fsimage、editlog、seen_txid说明
- 可以帮助提升程序员高效工作效率的常用小工具推荐
- 【软件测试技术】初识软件、软件测试
- python获取交换机信息
- 微信小程序系列开发(一)
- 魔兽世界终极版(C++程序设计第6周)
- 大象流的危害以及处理
- 编程乐趣--汉字转拼音
热门文章
- day15Java-Collection-集合顶层接口Collection
- Excel如何删除公式保留数值
- css101路由密码,192.168.0.101登陆官网登录入口
- flutter 获取appbar高度,状态栏高度
- 效果信息[gdc13]《刺客信条3》渲染技术
- Linux用户及文件权限管理
- DX9设备的一些方法介绍-------VB6编程学习DX9游戏编程DirectX9编程2D小游戏源码冷风引擎CoolWind2D游戏引擎(10)
- 云创地震大数据产品膺选“江苏省大数据产业发展试点示范项目”
- 定制着色器和渲染后期处理
- 【OLED屏】4针 OLED显示屏(IIC协议)底层驱动(库封装)(包含源文件)