通过URL推送POST数据_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 通过URL推送POST数据

通过URL推送POST数据

 2013/12/18 12:09:12  潜者之意  博客园  我要评论(0)
  • 摘要:由于到了一家新公司重新开始接触MVC和其他的一些东西。所以的重新拾起许多东西。前一段时间让我写一个和第三方公司推送对接的方法。通过对方提供的URL把数据post推送出去。我把url到了web.config里1<addkey="urlStrings"value="urladdress"/>ViewCode在.CS文件里1privatestringpostString=System.Configuration.ConfigurationManager
  • 标签:URL 数据

由于到了一家新公司重新开始接触MVC和其他的一些东西。所以的重新拾起许多东西。

前一段时间让我写一个和第三方公司推送对接的方法。通过对方提供的URL把数据post推送出去。

我把url到了web.config里

class="code_img_closed" src="/Upload/Images/2013121812/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('2cbaccd6-fa77-4626-8b42-422444b7a7f0',event)" src="/Upload/Images/2013121812/2B1B950FA3DF188F.gif" alt="" />
1  <add key="urlStrings" value="urladdress"/>
View Code

在.CS文件里

1  private string postString = System.Configuration.ConfigurationManager.AppSettings["urlStrings"].ToString();
View Code

因为我这边是把数据以xml文本的形式传送出去所以要对数据进行包装,然后通过HttpWebRequest请求发送数据。

 1                 string body = string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?>
 2 <Body>
 3 <ValidId>{0}</ValidId>
 4 <OrderId>{1}</OrderId>
 5 <Count>{2}</Count>
 6 <ValidTime>{3}</ValidTime>
 7 <Remark/>
 8 </Body>", consumption.Id, consumption.Order.AgentOrderId, consumption.Count, consumption.CreateTime.DateTimeToDateString("yyyy-MM-dd HH:mm:ss"));
 9                
10                 string request = BuildRequest(body);
11               
12                 HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(postString);
13                 hwr.Method = "POST";
14                 hwr.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode("openstack"));
15                 hwr.ContentType = "application/json";
16                 //hwr.Accept = "application/xml";
17                 hwr.AllowAutoRedirect = true;
18                
19                 byte[] dates = Encoding.UTF8.GetBytes(request);
20                 int count = dates.Length;
21                 //Stream newStream = hwr.GetRequestStream();
22                 MemoryStream newStream = new MemoryStream();
23                 try
24                 {
25                     log.Add("开始请求");
26                     newStream.Write(dates, 0, dates.Length);
27                     hwr.ContentLength = newStream.Length;
28                     Stream requestStream = hwr.GetRequestStream();
29                     newStream.Position = 0L;
30                     newStream.CopyTo(requestStream);
31                     newStream.Close();
32                     requestStream.Close();
33                    
View Code

在这个地方值得我注意的是刚开始的时候我最早的MemoryStream用的是Stream。但是Stream数据流会莫名的报错。Stream数据流不能进行length查找操作

后来我也是经过网上查找找了解决办法,用MemoryStream来暂代Stream,最后把Stream上的一些查找操作放在MemoryStream上来进行,最后再通过MemoryStream的CopyTo()方法将数据导入Stream数据流里。

最后的是数据的接收,这个就简单一些

1  HttpWebResponse hwResponse =(HttpWebResponse)hwr.GetResponse();
2                     Stream stream = null;
3                    stream= hwResponse.GetResponseStream();
4                     StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true);
5                     string file = reader.ReadToEnd();
6                     UTF8Encoding UTF = new UTF8Encoding();
7                     Byte[] Bytes = UTF.GetBytes(file);
8                     file = UTF.GetString(Bytes);
View Code

这个地方有一个对数据编码的转换,我是转为UTF-8编码。

最后的是我对接收数据的处理,因为我接收的也是xml文本形式的数据,所以还有做一些处理操作,也方便自己进行后续操作。

 1  HttpWebResponse hwResponse =(HttpWebResponse)hwr.GetResponse();
 2                     Stream stream = null;
 3                    stream= hwResponse.GetResponseStream();
 4                     StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default, true);
 5                     string file = reader.ReadToEnd();
 6                     UTF8Encoding UTF = new UTF8Encoding();
 7                     Byte[] Bytes = UTF.GetBytes(file);
 8                     file = UTF.GetString(Bytes);
 9 string strBody = TCodeServiceCrypt.Decrypt3DESFromBase64(GetElementValue(doc.Element("Response").Element("Body")), UserFunc.SecretKey);
10                         XDocument xBody = XDocument.Parse(strBody);
11                         string userId = GetElementValue(xBody.Element("Body").Element("UseId"));
View Code

这个就是我这次使用的一些应用。

发表评论
用户名: 匿名