新建新的空网站和一个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. suse linux11 包括所有的linux操作系统的 遗忘root密码解决方案

    2017-1-13号,用户要割接ocs系统应用,因为不能直接给root密码,但是操作过程中出现密码修改出错,再次登录系统仍然有问题.去机房熬了将近6个小时,试过单用户模式(但需要密码),试过光盘救援模 ...

  2. 如何得到AdoConnection.execute(sqlstr)执行的返回结果

    如何得到AdoConnection.execute(sqlstr)执行的返回结果? 1: TAdoConnection.execute有procedure.function的两种定义,function ...

  3. eclipse中svn提交过滤不需要的文件

    eclipse>Preference>Team>Ignored Resource 添加   .settings   .classpath   .project

  4. equals和hashcode为什么要一起重写

    equals()方法与hashCode()的通用协定是:2.1 如果两个对象相等(equal),那么必须拥有相同的哈希码(hash code)2.2 即使两个对象有相同的哈希值(hash code), ...

  5. 利用python3.5 +TK 开发股票自动交易伴侣

    # -*- encoding: utf8 -*- # version 1.11 import tkinter.messagebox,os from tkinter import * from tkin ...

  6. 克隆git仓库中的一个分支

    克隆git仓库中的某一个分支,可用如下命令: git clone -b <branch_name> <repo> 如:git clone -b hdcp_ree_tee_dev ...

  7. shrio初体验(2)Realm

    Realm:域,Shiro从从Realm获取安全数据(如用户.角色.权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法:也需 ...

  8. 【 Note 】GDB调试

    GDB是在linux下的调试功能 命令: 启动文件: 普通调试 gdb 可执行文件 分屏调试 gdb -tui 可执行文件 ->调试: 运行 r 设置断点 b 删除断点 delete 断点编号 ...

  9. C语言 · FJ的字符串

    问题描述 FJ在沙盘上写了这样一些字符串: A1 = "A" A2 = "ABA" A3 = "ABACABA" A4 = "AB ...

  10. line-height属性详解

    line-height属性详解:http://www.cnblogs.com/dolphinX/p/3236686.html