ASP.Net Post方式获取数据流的一种简单写法
最近在弄一些第三方的平台,经常调用第三方的接口实现某些特定的功能
在实现的同时基本上都需要本地的数据经过服务器在Request到第三方的服务器中处理,再返回相应的数据结构体:json/xml
以下是我总结的一个小方法,请农友们笑纳:
publicstaticstringPostWebReq(stringPostUrl,stringParamData,EncodingDataEncode)
{
stringret=string.Empty;
try
{
byte[]byteArray=DataEncode.GetBytes(ParamData);
HttpWebRequestwebReq=(HttpWebRequest)WebRequest.Create(newUri(PostUrl));
webReq.Method="POST";
webReq.ContentType="application/x-www-form-urlencoded";
webReq.ContentLength=byteArray.Length;
StreamnewStream=webReq.GetRequestStream();
newStream.Write(byteArray,0,byteArray.Length);
newStream.Close();
HttpWebResponseresponse=(HttpWebResponse)webReq.GetResponse();
StreamReadersr=newStreamReader(response.GetResponseStream(),DataEncode);
ret=sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch(WebExceptionex)
{
Log.WriteLog(LogFile.Error,ex.Message);
}
finally
{
Log.WriteLog(LogFile.Info,ret);
}
returnret;
}
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短