博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
参数请求post, get , delete中的基本使用(2)
阅读量:6152 次
发布时间:2019-06-21

本文共 3336 字,大约阅读时间需要 11 分钟。

UTF-8数字编码

 

///         /// 参数的Url请求        ///         /// 
public static string Anlv_RequestPostService(string PostData, string Url) { try { //设置HttpWebRequest基本信息 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(Url); myReq.AllowAutoRedirect = false; myReq.Method = "POST"; myReq.ContentType = "application/x-www-form-urlencoded"; myReq.KeepAlive = true; //把数组转换成流中所需字节数组类型 Encoding code = Encoding.GetEncoding("UTF-8"); byte[] bytesRequestData = code.GetBytes(PostData); //填充POST数据 myReq.ContentLength = bytesRequestData.Length; Stream requestStream = myReq.GetRequestStream(); requestStream.Write(bytesRequestData, 0, bytesRequestData.Length); requestStream.Close(); //发送POST数据请求服务器 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); Stream myStream = HttpWResp.GetResponseStream(); //获取服务器返回信息 StreamReader reader = new StreamReader(myStream, code); StringBuilder responseData = new StringBuilder(); String line; while ((line = reader.ReadLine()) != null) { responseData.Append(line); } //释放 myStream.Close(); return responseData.ToString(); } catch (Exception ex) { return ""; } }
View Code

 

 

GBK数字编码

 

///         /// 参数的Url请求        ///         /// 
public static string RequestPostService(string PostData,string Url) { try { string XMLPostData="xml=" + PostData+ ""; //设置HttpWebRequest基本信息 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(Url); myReq.AllowAutoRedirect = false; myReq.Method = "POST"; myReq.ContentType = "application/x-www-form-urlencoded"; myReq.KeepAlive = true; //把数组转换成流中所需字节数组类型 Encoding code = Encoding.GetEncoding("GBK"); byte[] bytesRequestData = code.GetBytes(XMLPostData); //填充POST数据 myReq.ContentLength = bytesRequestData.Length; Stream requestStream = myReq.GetRequestStream(); requestStream.Write(bytesRequestData, 0, bytesRequestData.Length); requestStream.Close(); //发送POST数据请求服务器 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); Stream myStream = HttpWResp.GetResponseStream(); //获取服务器返回信息 StreamReader reader = new StreamReader(myStream, code); StringBuilder responseData = new StringBuilder(); String line; while ((line = reader.ReadLine()) != null) { responseData.Append(line); } //释放 myStream.Close(); return responseData.ToString(); } catch (Exception ex) { return ""; } }
View Code

 

转载于:https://www.cnblogs.com/ly77461/p/5708280.html

你可能感兴趣的文章
linux 编码转换
查看>>
POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
查看>>
Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月7日-1月14日)
查看>>
关于C#导出 文本文件
查看>>
使用native 查询时,对特殊字符的处理。
查看>>
maclean liu的oracle学习经历--长篇连载
查看>>
ECSHOP调用指定分类的文章列表
查看>>
分享:动态库的链接和链接选项-L,-rpath-link,-rpath
查看>>
Javascript一些小细节
查看>>
禁用ViewState
查看>>
Android图片压缩(质量压缩和尺寸压缩)
查看>>
nilfs (a continuent snapshot file system) used with PostgreSQL
查看>>
【SICP练习】150 练习4.6
查看>>
HTTP缓存应用
查看>>
KubeEdge向左,K3S向右
查看>>
DTCC2013:基于网络监听数据库安全审计
查看>>
CCNA考试要点大搜集(二)
查看>>
ajax查询数据库时数据无法更新的问题
查看>>
Kickstart 无人职守安装,终于搞定了。
查看>>
linux开源万岁
查看>>