Asp.net HttpWebRequest和HttpWebResponse发送和接受任何类型数据
发送字符串数据
发送数据
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发送和接受任何类型数据的更多相关文章
- ASP.NET HttpWebRequest和HttpWebResponse
HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性. 模拟艺龙旅游网登录 想模拟登录,首先整理一下流程 1.通过360浏览器 ...
- springMVC接受json类型数据
springMVC接受json格式的数据很简单 使用@RequestBody 注解,标识从请求的body中取值 服务端示例代码 @RequestMapping(value = "/t4&qu ...
- Spring的controller接受Date类型数据,接受枚举类型数据
1. Controller接收Date类型的数据 核心使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 来将传递过来的时间字符串 ...
- ASP.NET通过http/https的POST方式,发送和接受XML文件内容
本文转载:http://hi.baidu.com/ysyhyt/item/5011ae39ce3cf49fb80c0395 本文参考:http://blog.csdn.net/ououou123456 ...
- 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 ...
- c# HttpWebRequest与HttpWebResponse 绝技(转载)
c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的Get和P ...
- c# HttpWebRequest与HttpWebResponse
[转]c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧. 本文章会对Http请求时的Get和 ...
- HttpWebRequest和HttpWebResponse
原文:http://blog.csdn.net/haitaofeiyang/article/details/18362225 申公前几日和一个客户做系统对接,以前和客户对接一般采用webservice ...
- 利用HttpWebRequest和HttpWebResponse获取Cookie
之前看过某个同学的一篇有关与使用JSoup解析学校图书馆的文章,仔细一看,发现竟然是同校!!既然对方用的是java,那么我也就来个C#好了,虽然我的入门语言是java. C#没有JSoup这样方便的东 ...
随机推荐
- springboot 学习笔记(二)
springboot 学习笔记(二) 快速创建一个springboot工程,并引入所需要的依赖 1.利用Spring initializr 来创建一个springboot项目,登陆http://sta ...
- BZOJ4299: Codechef FRBSUM(主席树)
题意 题目链接 数集S的ForbiddenSum定义为无法用S的某个子集(可以为空)的和表示的最小的非负整数. 例如,S={1,1,3,7},则它的子集和中包含0(S’=∅),1(S’={1}),2( ...
- The first step in solving any problem is recognizing there is one.
The first step in solving any problem is recognizing there is one.解决问题的第一步是要承认确实存在问题.
- Android仿360悬浮小球自定义view实现
转载请标明出处:http://www.jianshu.com/u/a5ad093cffe8 效果图如下: 图片.png 图片.png 实现当前这种类似的效果 (360小球 悬浮桌面差不错类似).第 ...
- OCX和DLL的区别
转自:http://blog.csdn.net/scucj/archive/2006/06/29/852181.aspx OCX和DLL的区别 一.关于DLL的介绍 DLL,动态链接库,Dy ...
- COGS 2342. [SCOI2007]kshort
★★☆ 输入文件:bzoj_1073.in 输出文件:bzoj_1073.out 简单对比时间限制:2 s 内存限制:512 MB [题目描述] 有n个城市和m条单向道路,城市编号为1 ...
- SpringMVC-常用的注解
1. RequestParam注解 把请求中的指定名称的参数传递给控制器中的形参赋值 value:请求参数中的名称 require:请求参数中是否必须提供此参数,默认值是true,必须提供 2. Re ...
- TFS2018 找不到JRE 错误
配置TFS 2018 server configurion 报错 : Search requires Oracle Server JRE 7 Update 55 or higher or JRE 8 ...
- Python 继承实现的原理(继承顺序)
继承顺序 Python3 : 新式类的查找顺序:广度优先 新式类的继承: class A(object): Python2 3 都是了 MRO算法--生成一个列表保存继承顺序表 不找到底部 Pytho ...
- Unity3d 判断物体是否在可见范围内
unity中自带两个回调函数: void OnBecameVisible()//当物体可见时,回调一次. void OnBecameInvisible()//当物体不可见时,回调一次. 在untiy编 ...