新建新的空网站和一个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. CodeForces 747E Comments

    栈,模拟. 手动写一个栈模拟一下过程即可. #include<cstdio> #include<cstring> #include<string> #include ...

  2. 浙大玉泉ubuntu L2TP VPN连接设置

    网络连接设置 1.内网有线 如果是笔记本且只用无线,剩下的就不需要看了.实验室台式机没有无线网卡不得不折腾-- 玉泉有线都是要绑定固定ip的,实验室无需和mac地址绑定,命令如下sudo gedit ...

  3. sql 时间格式化

    sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...

  4. 打印java堆栈信息

    使用如下命令: kill -3 {pid} 可以打印指定线程的堆栈信息到tomcat的catalina.out日志中.在性能测试过程中,可以观察响应时间的曲线,如果突然出现波峰则抓取当前时间点tomc ...

  5. edittext设置为密文显示

    et_msg.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);

  6. CSS继承性+层叠性+盒子+浮动

        CSS继承性+层叠性+盒子+浮动 CSS继承性 <style>         div{             color: pink;             font-siz ...

  7. CodeForces 677D Vanya and Treasure

    $dp$,树状数组. 很明显这是一个$DAG$上的$dp$,由于边太多,暴力$dp$会超时,需要优化. 例如计算$dp[x][y]$,可以将区域分成四块,$dp[x][y]$取四块中的最小值,每一块用 ...

  8. 博客停写,搬家到www.54kaikai.com

    博客搬家到自己的网站了www.54kaikai.com欢迎访问.

  9. 在MacOS下Python安装lxml报错xmlversion.h not found 报错的解决方案

    最近在看一个自动化测试框架的问题,需要用到Lxml库,下载lxml总是报错. 1,使用pip安装lxml pip install lxml 2,然后报错了,报错内容是: In file include ...

  10. ubuntu 14.04—解决软件中心进度条卡死的问题

    软件中心下载安装软件进度条卡住了,这时候解决方法为: 先解锁: sudo rm -rf /var/lib/dpkg/lock 如果此时开启软件中心,发现进度还在, 那么我们需要找到相关的进程关闭他,使 ...