新建新的空网站和一个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. readelf -s 命令‘symbol’名字显示不全

    GCC编译出来的object(目标文件)getPon.o,在链接时(ld)报了一个错误说找不到一个函数(undefined reference to identifier devCtl_getEthe ...

  2. ThreadPool

    private void button6_Click(object sender, EventArgs e) { ThreadPool.SetMinThreads(, ); ThreadPool.Se ...

  3. gc学习(转)

    一.GC特性以及各种GC的选择 1.垃圾回收器的特性 2.对垃圾回收器的选择 2.1 连续 VS. 并行 2.2 并发 VS. stop-the-world 2.3 压缩 VS. 不压缩 VS. 复制 ...

  4. Scala 中Null, None, Nothing, Nil

    转自:http://blog.csdn.net/bluejoe2000/article/details/30465175 在scala中这四个类型名称很类似,作用确实完全不同的. None是一个obj ...

  5. HC-05与HC-06的AT指令的区别

    蓝牙HC-05与HC-06对比指令集 高电平->AT命令响应工作状态     低电平->蓝牙常规工作状态 <重新上电表示完成复位> HC-05 可以主从切换模式,但是HC-06 ...

  6. Android Studio新手

    目标:Android Studio新手–>下载安装配置–>零基础入门–>基本使用–>调试技能–>构建项目基础–>使用AS应对常规应用开发 AS简介 经过2年时间的研 ...

  7. NEUQ1038: 谭浩强C语言(第三版)习题4.8

    之前没做对的一道题,今天集中清理一下. //------------------- 很水的题,主要是 %.2lf 不能四舍五入,需要仅保留两位小数,用了丑陋的强制类型转换... //--------- ...

  8. NET 2.0(C#)调用ffmpeg处理视频的方法

    另外:ffmpeg的net封装库 http://www.intuitive.sk/fflib/ NET 2.0 调用FFMPEG,并异步读取输出信息的代码...public void ConvertV ...

  9. eclipse 导入tomcat7源码

    导入tomcat的源码其实说简单也不简单,说不简单也简单,主要还是环境问题,中间花费了我很多时间,网上找了很多都没什么用,参考一些文章,然后自己慢慢摸索出来的. 环境:(1)jdk:jdk1.6.0_ ...

  10. sql convert() 函数

    convert: 时间格式转换为其他时间格式的函数 CONVERT ( data_type [ ( length ) ] , expression [ , style ] )   data_type: ...