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这样方便的东 ...
随机推荐
- js控制语句
1 条件判断语句 条件语句用于基于不同的条件来执行不同的动作. 1.1if 语句 if (condition){ 当条件为 true 时执行的代码} 1.2if...else 语句 if (co ...
- ES6 Interator
Interator "集合"数据的结构主要有 Array .Object. Set and Map ,任何数据结构只要部署 Iterator 接口,就可完成遍历操作 遍历过程: 创 ...
- npm相关命令
npm install npm install log4js npm list npm list log4js #查看模板安装版本 npm install log4js@1.0.1 #指定模块版本安装 ...
- selenium-Python之上传文件
对于web 页面的上传功能实现一般有一下两种方式 普通上传:普通的附件上传是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器 插件上传:一般是指基于flash.ja ...
- Securityonion介绍
下载地址 https://github.com/Security-Onion-Solutions/security-onion/blob/master/Verify_ISO.md ...
- Cocos2d-x——导入Cocostudio资源
(搬运自我在SegmentFault的博客) 目前正在和实训的小组成员一起做一款手机2D游戏,我们采用了Cocos2d-x进行开发.之前虽然早有耳闻,这次却是第一次认真地学习和使用Cocos2d-x. ...
- noip模拟赛#14
#14: T1:f[x]=x-1(x&1)||x/2(x&1=0) 求[n,m]有多少个数可以通过变换得到k.(1e9). =>好像cf上看过类似的题,用二进制的方式来写.不过我 ...
- event loop、进程和线程、任务队列
本文原链接:https://cloud.tencent.com/developer/article/1106531 https://cloud.tencent.com/developer/articl ...
- BOM属性对象方法
本文原链接:https://cloud.tencent.com/developer/article/1018747 BOM 1.window对象 2.location对象 3.history对象 BO ...
- 【离线 撤销并查集 线段树分治】bzoj1018: [SHOI2008]堵塞的交通traffic
本题可化成更一般的问题:离线动态图询问连通性 当然可以利用它的特殊性质,采用在线线段树维护一些标记的方法 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常 ...