新建新的空网站和一个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. 微信小程序入门——怎么建多个项目?(导入官方Demo程序进行学习)

    昨天1月9日微信小程序发布,顿时被朋友圈刷爆,今天看了一下官方文档,自己开始一步一步搭建环境体验小程序开发. 常见问题: 1.微信小程序开发是否需要重新创建开发者账号? 需要,即使之前申请了微信服务号 ...

  2. Windows 8 卡在正在检查更新

    原文地址:https://answers.microsoft.com/en-us/windows/forum/windows_8-update/windows-update-not-updating- ...

  3. 【SQL】T-SQL基本语法复习

    数据库基本的几个对象 数据表.视图.存储过程.索引.触发器.函数 增删改查 Insert into test(name,sex,ago) values ('陈三','男',20) Update tes ...

  4. 第一百三十三节,JavaScript,封装库--弹出登录框

    JavaScript,封装库--弹出登录框 封装库,增加了两个方法 yuan_su_ju_zhong()方法,将获取到的区块元素居中到页面,chuang_kou_shi_jian()方法,浏览器窗口事 ...

  5. redis面试总结

    http://blog.csdn.net/guchuanyun111/article/category/6335900 (1)什么是redis? Redis 是一个基于内存的高性能key-value数 ...

  6. 微信小程序-scroll-view隐藏滚动条

    在书写网页的时候,往往会为了页面的美观,而选择去掉滚动区域默认的滚动条,而在这里,就是为小程序去掉滚动条的其中的一种方法: scroll-view.wxml: scroll-view.wxss scr ...

  7. iOS Quartz2D画图

    对于刚接触Quartz2D的同学来说,先了解 上下文 的概念,再从最基础的画线来具体体验Quartz2D的画图步骤 介绍Quart2D :是苹果官方的二维(平面)绘图引擎,同时支持iOS和macOS系 ...

  8. 端口(port)的安全模式(security mode)

    1. Cisco29系列交换机可以做基于2层的端口安全 ,即mac地址与端口进行绑定.2. Cisco3550以上交换机均可做基于2层和3层的端口安全, 即mac地址与端口绑定以及mac地址与ip地址 ...

  9. HDU 4403 A very hard Aoshu problem(DFS)

    A very hard Aoshu problem Problem Description Aoshu is very popular among primary school students. I ...

  10. python json数组对象排序

    arr = [{"name": "name_1", "level": 1}, {"name": "name_2 ...