基本的.net中Post请求的写法
来源:互联网 发布:软件测试项目心得体会 编辑:程序博客网 时间:2024/05/16 08:08
这里要注意几个点:
第一就是编码,如果编码不对,容易中文乱码
第二就是ContentType 如果设置不对,有可能连方法都调试不进去(我api用的是MVC的普通controller)
第三就是
paramData参数形式要与ContentType保持一致
/// <summary> /// 发送POST请求 /// </summary> /// <param name="postUrl">api</param> /// <param name="paramData">参数,一般是param1=1¶m2=2这种形式</param> /// <param name="dataEncode">编码</param> /// <returns></returns> private string PostWebRequest(string postUrl, string paramData, Encoding dataEncode) { string ret = string.Empty; try { byte[] byteArray = dataEncode.GetBytes(paramData); //转化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; //webReq.ContentType = "application/json"; webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return ret; }
1 0
- 基本的.net中Post请求的写法
- 新东方API demo中get和post请求的写法
- asp.net中Get与Post请求的区别
- Android中利用HttpURLConnection发送Post请求并添加参数的写法
- jfinal框架中控制请求访问方式get or post 的拦截器写法
- Retrofit的基本post,get请求
- Android网络访问Post请求的两种写法
- Android发送post请求的两种写法(okHttp&&httpClient)
- jq中json请求的写法!
- Asp.net的MVC中如何避免POST请求中出现的重复提交
- Android中Okhttp进行get和post提交及数据请求的基本使用
- ApacheHttpClient中post和get的请求
- Action请求的写法
- web请求的写法
- GDI+中双缓冲的基本写法
- Ext中panel最基本的写法
- Asp.net mvc4 WebApi 中使用多个Post请求,无法识别的问题
- 安卓基本的POST请求工具类
- 指定 FTP 用户主目录与访问目录
- 修改CentOS7系统默认运行级别
- Intent的学习笔记
- 写文本文件
- 层序遍历二叉树
- 基本的.net中Post请求的写法
- 谁在说谎
- 请用递归和非递归俩种方法实现二叉树的前序遍历。
- 求树的深度
- C++文件读写详解(ofstream,ifstream,fstream
- SuperTab插件
- HDU-2553(行搜||DFS)
- 智能指针
- 用俩个栈实现队列