這篇文章主要講解了“.net怎么通過URL推送POST數(shù)據(jù)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“.net怎么通過URL推送POST數(shù)據(jù)”吧!
前一段時(shí)間讓我寫一個(gè)和第三方公司推送對(duì)接的方法。通過對(duì)方提供的URL把數(shù)據(jù)post推送出去。
我把url到了web.config里
復(fù)制代碼 代碼如下:
<add key="urlStrings" value="urladdress"/>
在.CS文件里
復(fù)制代碼 代碼如下:
private string postString = System.Configuration.ConfigurationManager.AppSettings["urlStrings"].ToString();
因?yàn)槲疫@邊是把數(shù)據(jù)以xml文本的形式傳送出去所以要對(duì)數(shù)據(jù)進(jìn)行包裝,然后通過HttpWebRequest請(qǐng)求發(fā)送數(shù)據(jù)。
復(fù)制代碼 代碼如下:
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("開始請(qǐng)求"); newStream.Write(dates, 0, dates.Length); hwr.ContentLength = newStream.Length; Stream requestStream = hwr.GetRequestStream(); newStream.Position = 0L; newStream.CopyTo(requestStream); newStream.Close(); requestStream.Close();
在這個(gè)地方值得我注意的是剛開始的時(shí)候我最早的MemoryStream用的是Stream。但是Stream數(shù)據(jù)流會(huì)莫名的報(bào)錯(cuò)。Stream數(shù)據(jù)流不能進(jìn)行l(wèi)ength查找操作
后來我也是經(jīng)過網(wǎng)上查找找了解決辦法,用MemoryStream來暫代Stream,最后把Stream上的一些查找操作放在MemoryStream上來進(jìn)行,最后再通過MemoryStream的CopyTo()方法將數(shù)據(jù)導(dǎo)入Stream數(shù)據(jù)流里。
最后的是數(shù)據(jù)的接收,這個(gè)就簡單一些
復(fù)制代碼 代碼如下:
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);
這個(gè)地方有一個(gè)對(duì)數(shù)據(jù)編碼的轉(zhuǎn)換,我是轉(zhuǎn)為UTF-8編碼。
最后的是我對(duì)接收數(shù)據(jù)的處理,因?yàn)槲医邮盏囊彩莤ml文本形式的數(shù)據(jù),所以還有做一些處理操作,也方便自己進(jìn)行后續(xù)操作。
復(fù)制代碼 代碼如下:
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)過本文的學(xué)習(xí)后,相信大家對(duì).net怎么通過URL推送POST數(shù)據(jù)這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
分享題目:.net怎么通過URL推送POST數(shù)據(jù)-創(chuàng)新互聯(lián)
本文來源:http://chinadenli.net/article18/dgpcdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站設(shè)計(jì)、云服務(wù)器、電子商務(wù)、網(wǎng)站建設(shè)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容