ASP.NET 最全的POST提交数据和接收数据 —— (1) 用url传参方式
//1、对象提交,字典方式
//接口方:public ActionResult GetArry(Car model)
public void PostResponse()
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://demo2.cm-force.com/appapi/apiaccount/aa");
Encoding encoding = Encoding.UTF8;
//string param = "UserName=123&UserPwd=123";//post 参数 Car c = new Car();
c.Passed = ;//true
c.LinkTel = "小测试";
c.CarBrand = "";
c.Loads = ;
c.UserId = ;
c.SortId = ;
c.CarArea = "";
c.CarStateId = ;
c.LinkMan = "";
c.Sfzh = "";
c.CarNum = "ABCDE1";
c.CarLength = ;
c.DrivingNum = "";
c.State = ;
c.CarId = ;
c.CarOwner = "";
c.CarAreaId = ;
c.CarTypeId = ;
c.AddTime = DateTime.Now; IDictionary<string, string> para = new Dictionary<string, string>();
para.Add("LinkTel", "第二次测试");
para.Add("CarBrand", "");
para.Add("Loads", "");
para.Add("UserId", "");
para.Add("SortId", ""); StringBuilder buffer = new StringBuilder();
int i = ;
foreach (string key in para.Keys)
{
if (i > )
{
buffer.AppendFormat("&{0}={1}", key, para[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, para[key]);
}
i++;
} //JavaScriptSerializer ser = new JavaScriptSerializer();
//string param = ser.Serialize(c);
byte[] bs = Encoding.UTF8.GetBytes(buffer.ToString()); string responseData = String.Empty;
req.Method = "POST";
//req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
reqStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd().ToString();
}
Response.Write(responseData);
}
} //2、链接方式提交数据
//接口方:public ActionResult GetArry(int UserId,string GroupName)
//提交参数:string param = "UserId=737&GroupName=一杯美酒";//post 参数
public void PostMethd()
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://demo2.cm-force.com/appapi/apiaccount/GetGroupsByUserId");
Encoding encoding = Encoding.UTF8;
string param = "userid=735";//post 参数 byte[] bs = Encoding.UTF8.GetBytes(param.ToString());
//byte[] bs = new byte[]{};
string responseData = String.Empty;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
req.ContentLength = bs.Length;
//return;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
reqStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd().ToString();
}
Response.Write(responseData);
} } //3、提交数组
//接口方:public ActionResult GetArry(string[] arrpost)
//提交参数:string param = "arrpost=737&arrpost=一杯美酒";//post 参数
public void PostArrMethd()
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:7242/appapi/apiaccount/GetArry");
Encoding encoding = Encoding.UTF8;
string param = "arrpost=737&arrpost=一杯美酒";//post 参数 byte[] bs = Encoding.UTF8.GetBytes(param.ToString());
//byte[] bs = new byte[]{};
string responseData = String.Empty;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
req.ContentLength = bs.Length;
//return;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
reqStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd().ToString();
}
Response.Write(responseData);
} } //4、提交数组对象
//接口方:public ActionResult GetArry(List<Car> arrpost)
/* 对象
public class Dasa
{
public Dasa() { }
private string name; public string Name
{
get { return name; }
set { name = value; }
}
private int age; public int Age
{
get { return age; }
set { age = value; }
}
}*/
//提交参数:string param = "arrpost[0].Name=737&arrpost[0].Age=23&arrpost[1].Name=一杯美酒&arrpost[1].Age=25";//post 参数
public void PostArrObjMethd()
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:7242/appapi/apiaccount/GetArry");
Encoding encoding = Encoding.UTF8;
string param = "arrpost[0].Name=737&arrpost[0].Age=23&arrpost[1].Name=一杯美酒&arrpost[1].Age=25";//post 参数 byte[] bs = Encoding.UTF8.GetBytes(param.ToString());
//byte[] bs = new byte[]{};
string responseData = String.Empty;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
req.ContentLength = bs.Length;
//return;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
reqStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd().ToString();
}
Response.Write(responseData);
} } #region 文件提交 //上传调用方法
public void upImg()
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("id", "TTR");
nvc.Add("btn-submit-photo", "Upload");
HttpUploadFile("http://demo2.cm-force.com/appapi/apiaccount/AddtUser", @"D:\1.jpg", "file", "image/jpeg", nvc);
} //5、提交文件
public string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
string result = string.Empty;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Timeout = ;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials; Stream rs = wr.GetRequestStream(); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, , boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, , formitembytes.Length);
}
rs.Write(boundarybytes, , boundarybytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, , headerbytes.Length); FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[];
int bytesRead = ;
while ((bytesRead = fileStream.Read(buffer, , buffer.Length)) != )
{
rs.Write(buffer, , bytesRead);
}
fileStream.Close(); byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, , trailer.Length);
rs.Close(); WebResponse wresp = null;
try
{//"路径:e:\\wwwroot\\wuliu\\wwwroot\\appapi\\apiaccount\\UploadFiles\\D:\\1.jpg"
//http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data/1924810#1924810
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2); result = reader2.ReadToEnd();
}
catch (Exception ex)
{
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
} return result;
} #endregion
#endregion
ASP.NET 最全的POST提交数据和接收数据 —— (1) 用url传参方式的更多相关文章
- Java基础知识强化之网络编程笔记06:TCP之TCP协议发送数据 和 接收数据
1. TCP协议发送数据 和 接收数据 TCP协议接收数据:• 创建接收端的Socket对象• 监听客户端连接.返回一个对应的Socket对象• 获取输入流,读取数据显示在控制台• 释放资源 TCP协 ...
- Java基础知识强化之网络编程笔记03:UDP之UDP协议发送数据 和 接收数据
1. UDP协议发送数据 和 接收数据 UDP协议发送数据: • 创建发送端的Socket对象 • 创建数据,并把数据打包 • 调用Socket对象的发送方法,发送数据包 • 释放资源 UDP协议接 ...
- WPF内实现与串口发送数据和接收数据
原文:WPF内实现与串口发送数据和接收数据 与串口发送数据和接收数据,在此作一个简单的Demo.此Demo可以实现按下硬件按钮,灯亮,发送灯状态数据过来.并且可以实现几个灯同时亮,发送灯的状态数据过来 ...
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理
话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性 ...
- STM32移植RT-Thread后的串口在调试助手上出现:(mq != RT_NULL) assert failed at rt_mq_recv:2085和串口只发送数据不能接收数据问题
STM32移植RT-Thread后的串口在调试助手上出现:(mq != RT_NULL) assert failed at rt_mq_recv:2085的问题讨论:http://www.rt-thr ...
- 说说ajax上传数据和接收数据
我是一个脑袋不太灵光的人,所以遇到问题,厚着脸皮去请教大神的时候,害怕被大神鄙视,但是还是被鄙视了.我说自己不要点脸面,那是不可能的,但是,为了能让自己的技术生涯能走的更长远一些,受点白眼,受点嘲笑也 ...
- Java Socket 服务端发送数据 客户端接收数据
服务端: package com.thinkgem.wlw.modules.api.test.socket; /** * @Author: zhouhe * @Date: 2019/4/8 9:30 ...
- MVC系列学习(五)-传递数据 与 接收数据
1.控制器向视图传递数据 a.使用ViewData b.使用ViewBag c.使用Model 方式二: d.使用TempData 2.为什么在控制器中设置了一些属性,在视图中可以接受 3.Actio ...
- asp.net core mvc视频A:笔记2-2.接收数据
传参方式一:使用内置方法传递 运行结果 其他获取方法 Session对象在HttpContext中 启用Session 使用Session 运行演示 传参方式二:数据绑定 普通类型(string).自 ...
随机推荐
- Python的scrapy之爬取顶点小说网的所有小说
闲来无事用Python的scrapy框架练练手,爬取顶点小说网的所有小说的详细信息. 看一下网页的构造: tr标签里面的 td 使我们所要爬取的信息 下面是我们要爬取的二级页面 小说的简介信息: 下面 ...
- Python文本操作2
# list3 = [# {"name": "alex", "hobby": "抽烟"},# {"name&q ...
- Java Integer == 以及分析
Java Integer == 先看一下这段代码 Integer integer1 = 100; Integer integer2 = 100; System.out.println("in ...
- java 写一个单例设计程序,打印出该对象的地址
class Test{ public static void main(String[] args) { singleton s1=singleton.getinstance(); singleton ...
- Prism for WPF 搭建一个简单的模块化开发框架(二)
原文:Prism for WPF 搭建一个简单的模块化开发框架(二) 今天又有时间了,再改改,加了一些控件全局的样式 样式代码 <ResourceDictionary xmlns="h ...
- windows 设置tomcat为自动启动服务
1.下载免安装tomcat包,解压 2.配置环境变量: 点击新建,创建一个 变量名为:CATALINA_HOME 变量值为:tomcat解压文件的位置, 例如 F:\apache-tomcat ...
- Java Dictionary Example
Dictionary class is the abstract class which is parent of any class which uses the key and value pai ...
- ONTAK 2010 aut
Autostrady https://szkopul.edu.pl/problemset/problem/f2dSBM7JteWHqtmVejMWe1bW/site/?key=statement 题意 ...
- LeetCode: 57. Insert Interval(Hard)
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
- VDI数据恢复
环境:cirtix xendesktop 问题:VDI无法正常启动,后台登录查看报错.多次重启无效果,客户部分数据存放在启动盘. 解决方法:1.创建一台新的VDI(必须保证关机)2.将原有VDI启动盘 ...