温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

.net怎么通过URL推送POST数据

发布时间:2021-07-26 13:34:48 来源:亿速云 阅读:266 作者:chen 栏目:开发技术

这篇文章主要讲解了“.net怎么通过URL推送POST数据”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“.net怎么通过URL推送POST数据”吧!

前一段时间让我写一个和第三方公司推送对接的方法。通过对方提供的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查找操作

.net怎么通过URL推送POST数据

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

感谢各位的阅读,以上就是“.net怎么通过URL推送POST数据”的内容了,经过本文的学习后,相信大家对.net怎么通过URL推送POST数据这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

url
AI