码迷,mamicode.com
首页 > 其他好文 > 详细

post方法

时间:2017-10-04 15:53:03      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:odi   mda   empty   app   ret   .text   help   key   amr   

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方法

标签:odi   mda   empty   app   ret   .text   help   key   amr   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!