萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> asp.net編程 >> .net 通過URL推送POST數據具體實現

.net 通過URL推送POST數據具體實現

 這篇文章主要介紹了.net 通過URL推送POST數據具體實現,有需要的朋友可以參考一下

由於到了一家新公司重新開始接觸MVC和其他的一些東西。所以的重新拾起許多東西。   前一段時間讓我寫一個和第三方公司推送對接的方法。通過對方提供的URL把數據post推送出去。   我把url到了web.config裡   代碼如下: <add key="urlStrings" value="urladdress"/>     在.CS文件裡   代碼如下: private string postString = System.Configuration.ConfigurationManager.AppSettings["urlStrings"].ToString();     因為我這邊是把數據以xml文本的形式傳送出去所以要對數據進行包裝,然後通過HttpWebRequest請求發送數據。 代碼如下: 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數據流會莫名的報錯。Stream數據流不能進行length查找操作   11.jpg   後來我也是經過網上查找找了解決辦法,用MemoryStream來暫代Stream,最後把Stream上的一些查找操作放在MemoryStream上來進行,最後再通過MemoryStream的CopyTo()方法將數據導入Stream數據流裡。   最後的是數據的接收,這個就簡單一些 代碼如下: 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);     這個地方有一個對數據編碼的轉換,我是轉為UTF-8編碼。   最後的是我對接收數據的處理,因為我接收的也是xml文本形式的數據,所以還有做一些處理操作,也方便自己進行後續操作。  代碼如下: 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"));     這個就是我這次使用的一些應用。   我是一個新手,請多指教。  
copyright © 萬盛學電腦網 all rights reserved