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).自 ...
随机推荐
- seleniun 爬取淘宝网
import re from selenium import webdriver from selenium.common.exceptions import TimeoutException fro ...
- django的Request-7
目录 1. 从url中获取截取 2. QueryDict (1). QueryDict.get(key, [default]) (2). QueryDict.getlist(key, [default ...
- Python(1-8天总结)
day1:变量:把程序运行过程中产生的中间值.暂时存储起来.方便后面的程序调用.变量命名规范:常量:所有字母大写注释:数据类型: 1. int 整数 2. str 字符串. 不会用字符串保存大量的数据 ...
- cannot import name '_imaging' 报错
例子如下: from PIL import Image import pytesseract as pt text = pt.image_to_string(Image.open("text ...
- 【EXCEL】XMLファイルを開く方法(XML文件打开方法)
前言 XMLとは:Extensible Markup Language(エクステンシブル マークアップ ランゲージ)は.基本的な構文規則を共通とすることで.任意の用途向けの言語に拡張することを容易とし ...
- Shell环境变量与特殊变量详解
1)变量类型 1)变量可分为俩类:环境变量(全局变量),和普通变量(局部变量), 环境变量也称为全局变量,可以在创建他们的Shell及其派生出来的任意子进程Shell中使用,环境变量又可以分为自定义环 ...
- 为 ItemsControl 类型的控件提供行号,mvvm模式 绑定集合
从网络上看到的两种方式,一种是,在 codebehind 里为 控件写事件,下面是将集合绑定到 DataGrid 控件: private void DataGridSoftware_LoadingRo ...
- Spring Cloud 分布式事务管理
Spring Cloud 分布式事务管理 在微服务如火如荼的情况下,越来越多的项目开始尝试改造成微服务架构,微服务即带来了项目开发的方便性,又提高了运维难度以及网络不可靠的概率. Spring Clo ...
- 北京Uber优步司机奖励政策(1月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Docker - vim安装
在使用docker容器时,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要安装vim. apt-get install vim 若提示: R ...