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这样方便的东 ...
随机推荐
- Java连接数据库,增删改查
底层代码: package com.zdsoft; import java.sql.*; /** * Created by lx on 2017/6/22. */ public class JDBCU ...
- hdfs校验和
hdfs完整性:用户希望储存和处理数据的时候,不会有任何损失或者损坏.所以提供了两种校验: 1.校验和(常用循环冗余校验CRC-32). 2.运行后台进程来检测数据块. 校验和: a.写入数据节点验证 ...
- My sql之存储过程+游标
sql 实例如下: /**************定义更改car_station_user_acct_his new_balance old_balance存储过程**************/ cr ...
- pc端常见布局---垂直居中布局 单元素不定高
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 你会如何给全局对象添加toString()方法
首先,在讨论如何给所有方法window对象添加toString方法的时候,我们先来说说window的对象继承与对象实例,以及构造函数的this指针,还有变量的提升与方法的调用方式,最终一探window ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
- eclipse报错GC overhead limit exceed,卡顿
在使用Eclipse的Build Project功能时,提示以下错误: An internal error occurred during: “Build Project”. GC overhead ...
- USACO08FEB Hotel
题目传送门 线段树维护区间 线段树结构体 struct zzz{ int l,r,mi; //l为以左端点的为起点的最长子串 //r为以右端点为终点的最长子串 //mi是区间内部的最长子串 }tree ...
- 使用CAShapeLayer实现复杂的View的遮罩效果
一.案例演示 最近在整理一个聊天的项目的时候,发送图片的时候,会有一个三角的指向效果,指向这张图片的发送者.服务端返回给我们的图片只是一张矩形的图片,我们如何把一张矩形的图片或者View,加上一层自定 ...
- SpringMVC 项目中引用其他 Module 中的方法
1. 将要引用的Module 引入项目中 2. 在主Module中添加依赖, 3. 被引用的类必须放在 Module 中/src/下的某个package中,否则引用不到(重要)