欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.net 通過URL推送POST數(shù)據(jù)具體實(shí)現(xiàn)

 更新時(shí)間:2013年12月18日 15:45:16   作者:  
這篇文章主要介紹了.net 通過URL推送POST數(shù)據(jù)具體實(shí)現(xiàn),有需要的朋友可以參考一下

由于到了一家新公司重新開始接觸MVC和其他的一些東西。所以的重新拾起許多東西。

前一段時(shí)間讓我寫一個(gè)和第三方公司推送對接的方法。通過對方提供的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文本的形式傳送出去所以要對數(shù)據(jù)進(jìn)行包裝,然后通過HttpWebRequest請求發(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("開始請求");
                    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ù)流會莫名的報(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è)對數(shù)據(jù)編碼的轉(zhuǎn)換,我是轉(zhuǎn)為UTF-8編碼。

最后的是我對接收數(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"));

這個(gè)就是我這次使用的一些應(yīng)用。

我是一個(gè)新手,請多指教。

相關(guān)文章

最新評論