.net 通过URL推送POST数据具体实现

所属分类: 网络编程 / ASP.NET 阅读数: 1986
收藏 0 赞 0 分享

由于到了一家新公司重新开始接触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查找操作

后来我也是经过网上查找找了解决办法,用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"));

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

我是一个新手,请多指教。

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多