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"));
//初始化新的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使用的更多相关文章
- 用WebRequest +HtmlAgilityPack 从外网抓取数据到本地
相信大家对于WebRequest 并不陌生,我们在C#中发请求的方式,就是创建一个WebRequest .那么如果我们想发一个请求到外网,比如国内上不了的一些网站,那么该怎么做呢? 其实WebRequ ...
- .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别
WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...
- c#利用WebClient和WebRequest获取网页源代码的比较
前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...
- WebRequest 访问 https
参考代码: 1: [TestMethod] 2: public void TestHttps() 3: { 4: var req =(HttpWebRequest) System.Net.WebReq ...
- WebRequest 获取网页乱码
问题:在用WebRequest获取网页源码时得到的源码是乱码. 原因:1,编码不对 解决办法:设置对应编码 WebRequest request = WebRequest.Create(Url);We ...
- WebClient与WebRequest差异
WebRequst的使用 WebClient和HttpWebRequst是用来获取数据的2种方式,在我的这篇数据访问(2)中主要是讲的WebClient的使用,一般而言,WebClient更倾向于“按 ...
- C#、.NET网络请求总结(WebClient和WebRequest)
1.关于WebClient第三方的封装,支持多文件上传等 using System; using System.Collections.Generic; using System.Text; usin ...
- 使用WebRequest 检测 手机号归属地。 C#通用 使用json 和可设定超时的WebClient
首先建立jsonObject,当然你也可以使用xml解析,目前介绍一下我使用的方法. /******************************************************** ...
- C#: Create a WebRequest with HTTP Basic Authentication
http://blog.csdn.net/huangyaoshifog/article/details/4470675 myReq = WebRequest.Create(url); string u ...
随机推荐
- respond.js
Respond.js,低版本浏览器也能够支持媒体查询 在之前有篇文章也是介绍IE6,7,8支持媒体查询的(查看),Respond.js这个比css3-mediaqueries更为强大一些,它可以支持l ...
- jquery回车执行某个事件
这里用到的是在查询框中输入数据后直接回车直接查询. //回车执行查询事件(执行class='btn-query'的单击事件) $(document).keydown(function (event) ...
- 2016huasacm暑假集训训练三 C - Til the Cows Come Home
题目链接:http://acm.hust.edu.cn/vjudge/contest/123674#problem/C N题目大意是有n个点,然后给出从a点到b点的距离,a和b是互相可以抵达的,则是无 ...
- BizTalk开发系列(十一) 在Orchestration中执行Pipeline
由于开发需要有时要在流程中执行Pipeline.比如从DB的某个字段中取消息的字符串并在流程中构造消息.该需要通过pipeline进行升级 属性字段,验证消息等处理.BizTalk架构已经开放了此接口 ...
- 关于static 的研究 与递归调用的输出
static的作用 :1.保存上次执行的结果 2.static int m; 这里默认m的初始值为0,即默认 值是0 #include "stdio.h" int fun(int ...
- Read4096
Given API: int Read4096(char* buf); It reads data from a file and records the position so that the n ...
- mongo vue的常用操作
查找在某个范围内的记录: {"_id":{$in: [a,b,c]}} 如果images是个数组,则查询方式与普通数据一样:{"images":&quo ...
- Registration Code
[sublime text 3] Michael BarnesSingle User LicenseEA7E-8213858A353C41 872A0D5C DF9B2950 AFF6F667C458 ...
- k8s入门系列之扩展组件(一)DNS安装篇
DNS (domain name system),提供域名解析服务,解决了难于记忆的IP地址问题,以更人性可读可记忆可标识的方式映射对应IP地址. Cluster DNS扩展插件用于支持k8s集群系统 ...
- Centos7安装配置NFS服务和挂载
现在有3台服务器 s1(主),s2(从), s3(从)需要实现文件实时同步,我们可以安装Nfs服务端和客户端来实现! 一.安装 NFS 服务器所需的软件包: 1 yum install -y nf ...