HttpWebRequest 以及WebRequest的使用
1.WebRequest的发送数据以及接收数据
class Program
{
static void Main(string[] args)
{
//创建一个实例并发送请求
HttpWebRequest we = (HttpWebRequest)WebRequest.Create("http://hao.360.cn/?src=360c");
we.Method = "Get";
we.KeepAlive = true;
//设置返回数据超时的时间30s
we.Timeout = ;
//开始异步获得发过来的数据
we.BeginGetResponse(new AsyncCallback(Callback), we); Console.ReadKey();
} static void Callback(IAsyncResult result)
{
HttpWebRequest we = (HttpWebRequest)result.AsyncState;
//结束获得的数据
WebResponse web = we.EndGetResponse(result);
//读取返回来的数据
using (Stream stream = web.GetResponseStream())
{
StreamReader streamReader = new StreamReader(stream);
string ss = streamReader.ReadToEnd();
Console.Write(ss);
}
}
}
2.HttpWebRequest的使用
class Program
{
static void Main(string[] args)
{
//创建一个请求实例
WebRequest web = (HttpWebRequest)HttpWebRequest.Create("http://www.baidu.com");
web.Method = "Post";
//开始异步发送请求数据
web.BeginGetRequestStream(new AsyncCallback(AsyncCallback), web);
Console.ReadKey();
} //发送数据 可以考虑发送登录的密码以及账号信息等
static void AsyncCallback(IAsyncResult result)
{
HttpWebRequest web = (HttpWebRequest)result.AsyncState;
using (Stream stream = web.EndGetRequestStream(result))
{
byte[] postData = Encoding.UTF8.GetBytes("你好");
stream.Write(postData, , postData.Length);
}
//开始异步接收数据
web.BeginGetResponse(new AsyncCallback(CallbackResponse), web); } //接收服务器返回的数据
static void CallbackResponse(IAsyncResult result)
{
HttpWebRequest web = (HttpWebRequest)result.AsyncState;
//结束返回的请求数据
HttpWebResponse response = (HttpWebResponse)web.EndGetResponse(result);
//输出返回的数据
using (Stream stream = response.GetResponseStream())
{
StreamReader streamReader = new StreamReader(stream);
//Console.WriteLine(streamReader.ReadToEnd()); File.WriteAllText(@"C:/12e.txt", streamReader.ReadToEnd(),Encoding.UTF8);
}
} }
HttpWebRequest 以及WebRequest的使用的更多相关文章
- .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别
WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...
- C#,WebRequest类、HttpWebRequest类与HttpRequest类的区别
C#,WebRequest类和HttpWebRequest类的区别? httpWebRequest是webRequest的子类,httpWebRequest是基于http协议的 . HttpWebRe ...
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需 要获取认证信息(如Cookie),所以返回的是HttpWeb ...
- Windows Phone 十五、HttpWebRequest
Windows 运行时中支持网络资源访问的对象:HttpWebRequest 对象 发送 GET/POST 请求,HttpHelper 封装,超时控制. HttpClient 对象 发送 GET/PO ...
- C# HttpWebRequest GET HTTP HTTPS 请求
下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需要获取认证信息(如Cookie),所以返回的是HttpWebResponse ...
- 通过HttpWebRequest请求与HttpWebResponse响应方式发布接口与访问接口
一.API接口的编码 1.首页的页面代码: protected void Page_Load(object sender, EventArgs e) { /* * 请求路径:http://xxxx/i ...
- 用HttpWebRequest提交带验证码的网站
using System; using System.Drawing; using System.IO; using System.Net; using System.Text; using Syst ...
- 使用HttpWebRequest发送自定义POST请求
平时用浏览器看网页的时候,点击一下submit按钮的时候其实就是给服务器发送了一个POST请求.但是如何在自己的C#程序里面实现类似的功能呢?本文给出了一个简单的范例,可以实现类似的和web serv ...
- 使用HttpWebrequest对网站进行模拟操作(附登陆百度demo)
// a[href=#viewSource]"); //查看源代码标签 viewSourceArr.attr("title", "查看源代码"); v ...
随机推荐
- Visual Studio Code Unit Testing
1.NUnit project.json { "version": "1.0.0-*", "testRunner": "nunit ...
- h5ai目录列表优化
h5ai是HTTP Web服务器的现代文件索引器,专注于您的文件.目录以有吸引力的方式显示,浏览它们通过不同的视图,面包屑和树状概述增强.最初,h5ai是HTML5 Apache Index的缩写,但 ...
- H5的localStorage简单存储删除
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- The android gradle plugin version 2.3.0-beta2 is too old, please update to the latest version.
编译项目的时候,报如下错误: Error:(, ) A problem occurred evaluating project ':app'. > Failed to apply plugin ...
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- mac系统下配置域名映射关系
1.cd /private/etc/ 先修改权限:sudo chmod 777 hosts vi hosts localhost:etc mhx$ cat hosts ## # Host Databa ...
- mac系统下显示隐藏文件
在终端上输入以下命令即可: 显示隐藏文件命令: defaults write com.apple.finder AppleShowAllFiles -bool true 不显示隐藏文件命令: defa ...
- BZOJ_1067_[SCOI2007]降雨量_ST表
BZOJ_1067_[SCOI2007]降雨量_ST表 Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意 Y<Z< ...
- hdu4975 A simple Gaussian elimination problem.(最大流+判环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 题意:和hdu4888基本一样( http://www.cnblogs.com/a-clown/ ...
- bzoj 1084: [SCOI2005]最大子矩阵【dp】
分情况讨论,m=1的时候比较简单,设f[i][j]为到i选了j个矩形,前缀和转移一下就行了 m=2,设f[i][j][k]为1行前i个,2行前j个,一共选了k个,i!=j的时候各自转移同m=1,否则转 ...