C#post方法

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; } } }
原文地址:https://www.cnblogs.com/a735882640/p/7625638.html