新建新的空网站和一个default.aspx页面测试,实验例子:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpWebRequest web = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/"); //创建一个请求
web.Method = "GET"; //方式为get
web.ContentType = "text/html;charset=UTF-8"; //定义获取数据的类型
HttpWebResponse response = (HttpWebResponse)web.GetResponse(); //获取响应
string result = "";
using (Stream stream = response.GetResponseStream()) //定义缓存,将数据读取出来
{
StreamReader sr = new StreamReader(stream);
result = sr.ReadToEnd();
}
Label1.Text = result.ToString(); }
}

二,客户端带参和带文件流的请求方法

        #region HTTP请求
/// <summary>
/// HTTP请求接口
/// </summary>
/// <param name="UploadFile"></param>
/// <returns></returns>
public ActionResult HZRequest2(HttpPostedFileBase UploadFile)
{
string key = "PKWodTrwuTXoMvRUZhUgyf";
string url = "http://localhost:5832/Home/HZDistinguish?key=" + key + "";
byte[] byt = new byte[UploadFile.InputStream.Length];
UploadFile.InputStream.Read(byt, , (int)UploadFile.InputStream.Length);
//初始化新的webRequst
//1. 创建httpWebRequest对象
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
//2. 初始化HttpWebRequest对象
webRequest.Method = "POST";
webRequest.ContentType = "multipart/form-data";
webRequest.ContentLength = byt.Length;
//3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。)
Stream newStream = webRequest.GetRequestStream();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 newStream.Write(byt, , byt.Length);
newStream.Close();
//4. 读取服务器的返回信息
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader stmRead = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string reapond = stmRead.ReadToEnd();
response.Close();
stmRead.Close();
return Content(reapond);
}
#endregion

服务器接收方法:

        public ActionResult HZDistinguish()
{
int lang = Request.TotalBytes;
byte[] bytes = Request.BinaryRead(lang); Stream stream = Request.InputStream;
string key = Request["key"];
}

三,Post带参数的方法

        public static string Post(string url, Dictionary<string, object> dic)
{
StringBuilder str = new StringBuilder();
foreach (KeyValuePair<string, object> kv in dic)
{
string pkey = kv.Key;
object pvalue = kv.Value;
str.Append("&" + pkey + "=" + pvalue);
}
var data = Encoding.ASCII.GetBytes(str.ToString()); //初始化新的webRequst
//1. 创建httpWebRequest对象
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
//2. 初始化HttpWebRequest对象
webRequest.Method = "Post";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = data.Length; using (var stream = webRequest.GetRequestStream())
{
stream.Write(data, , data.Length);
}
//4. 读取服务器的返回信息
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); StreamReader stmRead = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = stmRead.ReadToEnd();
response.Close();
stmRead.Close();
return result;
}

HttpWebRequest的简单使用的更多相关文章

  1. HttpWebRequest HttpClient

    HttpWebRequest HttpClient 简单封装使用,支持https HttpWebRequest using System; using System.Collections.Gener ...

  2. .NET 爬虫总结

    前言 技术本身并无罪,罪恶本质在于人心,好比厨师手中的菜刀用来创造美味佳肴,而杀手手上的刀却用来创造无限的罪恶. 环境 win7 IIS 6.0  SQLserver2012 .NET 4.0 win ...

  3. HttpWebRequest简单使用

    HttpWebRequest简单使用  摘要 HttpWebRequest类对WebRequest中定义的属性和方法提供支持,也对使用户能够直接与使用HTTP的服务器交互的附加属性和方法提供支持. 创 ...

  4. HttpWebRequest post 提交 C#的WebBrowser操作frame如此简单 WebClient 提交

    //http://www.cnblogs.com/cgli/archive/2011/04/09/2010497.html System.Net.ServicePointManager.Expect1 ...

  5. C#使用HttpWebRequest发送数据和使用HttpWebResponse接收数据的一个简单示例

    新建一个.NET Core控制台项目,代码如下所示: using System; using System.Text; using System.Net; using System.Collectio ...

  6. .NET Web开发技术简单整理

    在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何使用该技术,如何更好的使用该技术解决问题,而没有去关注它的相关性.关注它的理论支持,这种学习技术的方式是短平快.其实工作中有时候也是这样,公 ...

  7. 在使用 HttpWebRequest Post数据时候返回 400错误

    笔者有一个项目中用到了上传zip并解压的功能.开始觉得很简单,因为之前曾经做过之类的上传文件的功能,所以并不为意,于是使用copy大法.正如你所料,如果一切很正常的能运行的话就不会有这篇笔记了. 整个 ...

  8. C#开发微信公众平台-就这么简单(附Demo)

    写在前面 阅读目录: 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文.菜单事件响应) 示例Demo下载 后记 最近公司在做微信开发,其实就是接口开发,网上找了很多资料, ...

  9. Xamarin.Android之封装个简单的网络请求类

    一.前言 回忆到上篇 <Xamarin.Android再体验之简单的登录Demo> 做登录时,用的是GET的请求,还用的是同步, 于是现在将其简单的改写,做了个简单的封装,包含基于Http ...

随机推荐

  1. Caffe安装过程错误处理方法

    1. 错误1: fatal error: caffe/proto/caffe.pb.h: No such file or directory 解决方法: You need to generate ca ...

  2. ORA-01940无法删除当前已连接用户

    原文地址:ORA-01940无法删除当前已连接用户作者:1736188794 1)查看用户的连接状况 select username,sid,serial# from v$session ------ ...

  3. 移动APP脚本录制

    1.安装补丁--LR_03105_patch4----mobile app(http/html) 2.录制软件和移动设备同处同一环境(160wifi连接移动设备),创建wifi热点 3.创建脚本-协议 ...

  4. 修改/etc/resolv.conf又恢复到原来的状态?[转]

    新装一台机器环境为服务器主板,双网卡,系统为CentOS5.4 ,eth0为内网ip,eth1为公网ip.但是由于在本地测试,设置的内网ip,域名服务器同样使用的是上海本地的域名解析,没有问题,可以上 ...

  5. install plugin elasticsearch-analysis-ik

    1.github下载分词器插件(请各位下载自己elasticsearch相对应的版本,否则会有兼容性问题) https://github.com/medcl/elasticsearch-analysi ...

  6. mysql报错1024-can't get hostname for your address

    前一段时间mysql用的好好的,突然一天,mysql启动后,在使用navicat连接数据库的时候 报错1024-can't get hostname for your address 这里我是这样解决 ...

  7. web前端入门

    看到很多同学在咨询:学习前端该怎么入门啊.推荐一下前端入门书籍啊什么的,作为一个过来人,我想告诉你一些小小技巧,避免走弯路: 1.先敲再学.如果你是零基础,就不要去每个标签,每个属性地去抠,因为里面有 ...

  8. 深入理解Solaris内核中互斥锁(mutex)与条件变量(condvar)之协同工作原理

    在Solaris上写内核模块总是会用到互斥锁(mutex)与条件变量(condvar), 光阴荏苒日月如梭弹指一挥间,Solaris的大船说沉就要沉了,此刻心情不是太好(Orz).每次被年轻的有才华的 ...

  9. memcahced缓存特点

    1.key-value数据结构 2.所有数据保存在内存中 3.可以分布式集群 4.处理并发的机制是libevent事件机制 5.当内容容量达到指定值后,就基于LRU(Least Recently Us ...

  10. find用法积累

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...