post方法
Posted 735882640
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } } }
以上是关于post方法的主要内容,如果未能解决你的问题,请参考以下文章