這篇文章主要講解了“.net怎么通過URL推送POST數(shù)據(jù)”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“.net怎么通過URL推送POST數(shù)據(jù)”吧!

前一段時間讓我寫一個和第三方公司推送對接的方法。通過對方提供的URL把數(shù)據(jù)post推送出去。
我把url到了web.config里
復制代碼 代碼如下:
<add key="urlStrings" value="urladdress"/>
在.CS文件里
復制代碼 代碼如下:
private string postString = System.Configuration.ConfigurationManager.AppSettings["urlStrings"].ToString();
因為我這邊是把數(shù)據(jù)以xml文本的形式傳送出去所以要對數(shù)據(jù)進行包裝,然后通過HttpWebRequest請求發(fā)送數(shù)據(jù)。
復制代碼 代碼如下:
string body = string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?>
<Body>
<ValidId>{0}</ValidId>
<OrderId>{1}</OrderId>
<Count>{2}</Count>
<ValidTime>{3}</ValidTime>
<Remark/>
</Body>", consumption.Id, consumption.Order.AgentOrderId, consumption.Count, consumption.CreateTime.DateTimeToDateString("yyyy-MM-dd HH:mm:ss"));
string request = BuildRequest(body);
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(postString);
hwr.Method = "POST";
hwr.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode("openstack"));
hwr.ContentType = "application/json";
//hwr.Accept = "application/xml";
hwr.AllowAutoRedirect = true;
byte[] dates = Encoding.UTF8.GetBytes(request);
int count = dates.Length;
//Stream newStream = hwr.GetRequestStream();
MemoryStream newStream = new MemoryStream();
try
{
log.Add("開始請求");
newStream.Write(dates, 0, dates.Length);
hwr.ContentLength = newStream.Length;
Stream requestStream = hwr.GetRequestStream();
newStream.Position = 0L;
newStream.CopyTo(requestStream);
newStream.Close();
requestStream.Close();在這個地方值得我注意的是剛開始的時候我最早的MemoryStream用的是Stream。但是Stream數(shù)據(jù)流會莫名的報錯。Stream數(shù)據(jù)流不能進行l(wèi)ength查找操作

后來我也是經(jīng)過網(wǎng)上查找找了解決辦法,用MemoryStream來暫代Stream,最后把Stream上的一些查找操作放在MemoryStream上來進行,最后再通過MemoryStream的CopyTo()方法將數(shù)據(jù)導入Stream數(shù)據(jù)流里。
最后的是數(shù)據(jù)的接收,這個就簡單一些
復制代碼 代碼如下:
HttpWebResponse hwResponse =(HttpWebResponse)hwr.GetResponse(); Stream stream = null; stream= hwResponse.GetResponseStream(); StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true); string file = reader.ReadToEnd(); UTF8Encoding UTF = new UTF8Encoding(); Byte[] Bytes = UTF.GetBytes(file); file = UTF.GetString(Bytes);
這個地方有一個對數(shù)據(jù)編碼的轉(zhuǎn)換,我是轉(zhuǎn)為UTF-8編碼。
最后的是我對接收數(shù)據(jù)的處理,因為我接收的也是xml文本形式的數(shù)據(jù),所以還有做一些處理操作,也方便自己進行后續(xù)操作。
復制代碼 代碼如下:
HttpWebResponse hwResponse =(HttpWebResponse)hwr.GetResponse();
Stream stream = null;
stream= hwResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true);
string file = reader.ReadToEnd();
UTF8Encoding UTF = new UTF8Encoding();
Byte[] Bytes = UTF.GetBytes(file);
file = UTF.GetString(Bytes);
string strBody = TCodeServiceCrypt.Decrypt3DESFromBase64(GetElementValue(doc.Element("Response").Element("Body")), UserFunc.SecretKey);
XDocument xBody = XDocument.Parse(strBody);
string userId = GetElementValue(xBody.Element("Body").Element("UseId"));感謝各位的閱讀,以上就是“.net怎么通過URL推送POST數(shù)據(jù)”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對.net怎么通過URL推送POST數(shù)據(jù)這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設公司,,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
分享題目:.net怎么通過URL推送POST數(shù)據(jù)-創(chuàng)新互聯(lián)
本文來源:http://chinadenli.net/article18/dgpcdp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站設計、云服務器、電子商務、網(wǎng)站建設、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容