• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    .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查找操作

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

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

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

    您可能感兴趣的文章:
    • 基于SignalR的消息推送与二维码扫描登录实现代码
    • Asp.NET MVC中使用SignalR实现推送功能
    • 使用SignalR推送服务在Android的实现 SignalA
    • asp.net mvc实现简单的实时消息推送
    • ASP.NET实现推送文件到浏览器的方法
    • .net平台推送ios消息的实现方法
    • SignalR Self Host+MVC等多端消息推送服务(二)
    • SignalR Self Host+MVC等多端消息推送服务(一)
    • SignalR Self Host+MVC等多端消息推送服务(三)
    上一篇:手把手教你在.NET中创建Web服务实现方法
    下一篇:asp.net微软图表控件使用示例代码分享
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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