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这样方便的东 ...
随机推荐
- jsp使用中$的符号使用失效
解决方法 添加一段话 <%@ page isELIgnored="false"%> 原因:因为jsp servlet版本问题,2.3及2.3之前的版本isELIgno ...
- JavaScprit30-6 学习笔记
今天学习的是 仿即时搜索诗句效果 第一个问题: fetch() Fetch API 提供了一个 JavaScript接口,用于访问和操纵HTTP管道的部分,例如请求和响应.它还提供了一个全局 fe ...
- azure powershell 获取可用镜像列表
通过Azure Powershell 指定location和Pbulishername 获取所有可用镜像的 publisherName,Offer,Skus,Version,location信息列表 ...
- SQLServer 2012 报表服务部署配置(2)
2.当系统打开"SQL Server安装中心",则说明我们可以开始正常的安装SQL Server 2012,可以通过"计划"."安装".&q ...
- Python+selenium之截图图片并保存截取的图片
本文转载:http://blog.csdn.net/u011541946/article/details/70141488 http://www.cnblogs.com/timsheng/archiv ...
- HDU 2089 不要62 (数位DP,入门)
题意: 只要含连续的62,或者含4的车牌号码都是不吉利的,其他都是吉利的组合.问区间[L,R]中有多少个数是吉利的? 思路: 依然是利用树(10进制是十叉树)的思想,统计左边所有子树有多少个数是吉利的 ...
- MyEclipse Update Progress Error解决方法
MyEclipse Update Progress Error现象:频繁弹出Update Progress(Time of error:……)窗口..log文件里 org.eclipse.swt.SW ...
- tpcc-mysql运行结果解读
前言 首先我们需要知道tpcc-mysql是干什么的.TPC-C是专门针对联机交易处理系统(OLTP系统)的规范,一般情况下我们也把这类系统称为业务处理系统.tpcc-mysql是percona基于T ...
- python_89_configparser模块
用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为ConfigPsresr 来看一个好多软件的常见文档格式如下 [ ...
- mysql 从陌生到熟练之----数据库备份恢复的实现方法
mysql 从陌生到熟练之----数据库备份恢复的实现方法 MySQL数据库使用命令行备份|MySQL数据库备份命令 例如: 数据库地址:127.0.0.1 数据库用户名:root 数据库密码:roo ...