//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

最新文章

  1. Node.js:DNS模块的使用
  2. iOS Storyboard全解析
  3. sqlserver表数据导出为insert into语句
  4. Cmd命令
  5. 关于SQL SERVER的N前缀的理解
  6. Python3基础 多分支结构 if-elif-else
  7. 【转】Asp.net中时间格式化的6种方法详细总结
  8. Request对象的主要方法
  9. C#操作Excel基本操作
  10. Unity3D脚本中文系列教程(十六)
  11. 改变linux shell前景色和背景色
  12. SVN服务器的搭建 分类: 网络 2014-11-27 01:18 204人阅读 评论(4) 收藏
  13. la 3942 Rember_前缀树
  14. linux培训笔记1
  15. Linux驱动基础:MSM平台AP/CP通信机制
  16. JAVA 类的三大特性,封装,继承,多态 的一些发现总结
  17. .net 上传文件大小限制
  18. 红黑树与AVL
  19. 关于SAN和NAS的区别-转
  20. vue基础——组件基础

热门文章

  1. Spark 加载数据库mysql表中数据进行分析
  2. 面试:Hbase和Hive的区别
  3. python七类之整型布尔值
  4. GDB简单使用
  5. UART学习之路(二)基本时序介绍
  6. 20145234黄斐《信息安全系统设计基础》第八周(Linux下vim相关命令)
  7. 全国Uber优步司机奖励政策 (12月28日-1月3日)
  8. 韩国KT软件NB-IOT开发记录V150(2)IOT maker通信相关
  9. eclipse+tomcat配置远程debug调整
  10. 使用CRF做命名实体识别(三)