发送字符串数据
发送数据

string strId = "guest";
string strPassword = ""; string postData = "userid=" + strId;
postData += ("&password=" + strPassword); byte[] data = Encoding.UTF8.GetBytes(postData); // Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:8058/PostResult.aspx"); myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream(); // Send the data.
newStream.Write(data, , data.Length);
newStream.Close(); // Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
Response.Write(content);

接收数据

Stream s = Request.InputStream;
StreamReader sr = new StreamReader(s);
string ss = sr.ReadToEnd();
Response.Write(ss);

发送任意类型数据
发送数据

//当前页面地址

            string currentUrl = Request.Url.ToString();

            string fileName = "复制文件";

            string url = currentUrl.Substring(, currentUrl.LastIndexOf('/')) + "/Default2.aspx?id=" + fileName;   //发送到的页面的地址

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            //读取一个文件

            FileStream fs = new FileStream(Server.MapPath("程序更新说明书.doc"), System.IO.FileMode.Open, System.IO.FileAccess.Read);

            byte[] filecontent = new byte[fs.Length];

            fs.Read(filecontent, , filecontent.Length);

            fs.Close();

            fs.Dispose();

            //将图片转换成base64编码的流

            string a = Convert.ToBase64String(filecontent);

            //读取base64编码流,发送

            byte[] requestBytes = System.Text.Encoding.Default.GetBytes(a);

            req.Method = "POST";

            req.ContentType = "application/x-www-form-urlencoded";

            req.ContentLength = requestBytes.Length;

            Stream requestStream = req.GetRequestStream();

            requestStream.Write(requestBytes, , requestBytes.Length);

            requestStream.Close();

            //接收返回参数,到string backstr

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();

            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);

            string backstr = sr.ReadToEnd();

            sr.Close();

            res.Close();

            //输出参数

            Response.Write(backstr);

接收数据

//接收到的参数
string bb = Request.QueryString["id"]; Encoding myEncoding = Encoding.GetEncoding("utf-8"); //接收传递过来的数据流 Stream resStream = Request.InputStream; byte[] filecontent = new byte[resStream.Length]; //将数据流读入byte数组 resStream.Read(filecontent, , filecontent.Length); //数组转换为string以便转换base64使用 string a = myEncoding.GetString(filecontent); //将string读取base64解密到byte数组 byte[] filecontent2 = Convert.FromBase64String(a); //写入目录 File.WriteAllBytes(Server.MapPath(bb + ".doc"), filecontent2); //返回值 Response.Write("ok"); Response.End();

来源:北京网站建设-恒动时空

Asp.net HttpWebRequest和HttpWebResponse发送和接受任何类型数据的更多相关文章

  1. ASP.NET HttpWebRequest和HttpWebResponse

    HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性. 模拟艺龙旅游网登录 想模拟登录,首先整理一下流程 1.通过360浏览器 ...

  2. springMVC接受json类型数据

    springMVC接受json格式的数据很简单 使用@RequestBody 注解,标识从请求的body中取值 服务端示例代码 @RequestMapping(value = "/t4&qu ...

  3. Spring的controller接受Date类型数据,接受枚举类型数据

    1. Controller接收Date类型的数据 核心使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 来将传递过来的时间字符串 ...

  4. ASP.NET通过http/https的POST方式,发送和接受XML文件内容

    本文转载:http://hi.baidu.com/ysyhyt/item/5011ae39ce3cf49fb80c0395 本文参考:http://blog.csdn.net/ououou123456 ...

  5. C# -- HttpWebRequest 和 HttpWebResponse 的使用 C#编写扫雷游戏 使用IIS调试ASP.NET网站程序 WCF入门教程 ASP.Net Core开发(踩坑)指南 ASP.Net Core Razor+AdminLTE 小试牛刀 webservice创建、部署和调用 .net接收post请求并把数据转为字典格式

    C# -- HttpWebRequest 和 HttpWebResponse 的使用 C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebReq ...

  6. c# HttpWebRequest与HttpWebResponse 绝技(转载)

    c# HttpWebRequest与HttpWebResponse 绝技    如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的Get和P ...

  7. c# HttpWebRequest与HttpWebResponse

    [转]c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧. 本文章会对Http请求时的Get和 ...

  8. HttpWebRequest和HttpWebResponse

    原文:http://blog.csdn.net/haitaofeiyang/article/details/18362225 申公前几日和一个客户做系统对接,以前和客户对接一般采用webservice ...

  9. 利用HttpWebRequest和HttpWebResponse获取Cookie

    之前看过某个同学的一篇有关与使用JSoup解析学校图书馆的文章,仔细一看,发现竟然是同校!!既然对方用的是java,那么我也就来个C#好了,虽然我的入门语言是java. C#没有JSoup这样方便的东 ...

随机推荐

  1. 【Linux】Ubuntu配置zshell&oh-my-zsh

    zshell:https://archive.codeplex.com/?p=shell oh-my-zsh: https://github.com/robbyrussell/oh-my-zsh 终极 ...

  2. Promise 对象与Generator 函数

    异步编程的方法,大概有下面四种: 回调函数 事件监听 发布/订阅 Promise 对象 传统的编程语言,早有异步编程的解决方案(其实是多任务的解决方案).其中有一种叫做"协程"(c ...

  3. filter和map的使 使得数组对象变数组

    let UnitList = this.paytypeData.filter( item => item.CheckBox === true ).map(axis => axis.Unit ...

  4. MySQL memory引擎 table is full 问题处理

    解决mysql的内存表“table is full”错误   101209 13:13:32 [ERROR] /usr/local/mysql/bin/mysqld: The table ‘test_ ...

  5. shell命令cut

    cut命令用来操作字符串,可以理解为剪切字符串的工具: cut有两种用法: 1.剪切字符串中的单个字符(-c参数) 例如: str=abcdef echo $str | cut -c 1-1 输出:a ...

  6. BZOJ 4070:[APIO2015]雅加达的摩天楼 最短路

    4070: [Apio2015]雅加达的摩天楼 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 464  Solved: 164[Submit][Sta ...

  7. 前端面试题总结(三)JavaScript篇

    前端面试题总结(三)JavaScript篇 一.谈谈对this的理解? this是一个关键字. this总是指向函数的直接调用者(而非间接调用者). 如果有new关键字,this指向new出来的那个对 ...

  8. Ubuntu18.04如何从英文界面更改为中文界面

    本文介绍如何将Ubuntu18.04安装后的英文界面,更改为中文界面,即系统语言由英文改为简体中文.注意,与安装中文输入法不同,两者也没有冲突. 首先进入设置(Setting),选择区域和语言(Reg ...

  9. 通用的flash代码

    黑体字部分为常修改的部分 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=&quo ...

  10. linux readahead

    blockdev --getra /dev/sda blockdev --setra 2048 /dev/sda 必须将其写入配置文件/etc/rc.local,否则重启就会失效.[root@loca ...