首页 > 其他 > 详细

post方法

时间:2017-10-04 15:42:50      阅读:225      评论:0      收藏:0      [点我收藏+]
post方法
using
System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; namespace Demo { public class MyPostGetHelper { public static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode, string header = "") { string ret = string.Empty; try { if (dataEncode == null) dataEncode = Encoding.UTF8; byte[] byteArray = dataEncode.GetBytes(paramData); //转化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/json"; webReq.Headers.Add("Session-Key", header); webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); var c = response.GetResponseStream(); StreamReader sr = new StreamReader(c, Encoding.UTF8); Char[] read = new Char[4000]; int count = sr.Read(read, 0, read.Length); while (count > 0) { String str = new String(read, 0, count); ret += str; count = sr.Read(read, 0, read.Length); } sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { throw; } return ret; } } }

 

post方法

原文:http://www.cnblogs.com/a735882640/p/7625638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!