C#-httpPOST请求
(2018-09-13 16:06:30)
private static string PostWebRequest(string
postUrl, string paramData, Encoding dataEncode = null)
{
string ret =
string.Empty;
byte[]
byteArray = (dataEncode ??
Encoding.UTF8).GetBytes(paramData);//转化
HttpWebRequest
webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method
= "POST";
webReq.Accept
= "application/text";//返回类型
webReq.ContentType
= "application/x-www-form-urlencoded";//调用类型
webReq.ContentLength
= byteArray.Length;
using (Stream
newStream = webReq.GetRequestStream())
{
newStream.Write(byteArray,
0, byteArray.Length);//写入参数
using
(HttpWebResponse response =
(HttpWebResponse)webReq.GetResponse())
{
using
(StreamReader sr = new StreamReader(response.GetResponseStream(),
dataEncode ?? Encoding.UTF8)) ret = sr.ReadToEnd();
}
}
return
ret;
} |
喜欢
0
赠金笔
加载中,请稍候......