zoukankan      html  css  js  c++  java
  • 通过URL推送POST数据

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

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

    我把url到了web.config里

    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

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

  • 相关阅读:
    白雪公主与七个爷爷——冲刺日志(第二天)
    vba实践
    微信小程序scroll-view不起作用,bindscrolltolower事件无响应
    git bash相关命令
    hexo博客中命令备忘(markdown语法)
    微信聊天记录生成词云图(等日后有机会补充完整)
    微信小程序解决存在图片太大而有效显示区域较小--阿里云oss产生缩略图
    css样式基础库--global.css(持续更新中~)
    Chrome控制台常用技巧
    css基础-动画效果
  • 原文地址:https://www.cnblogs.com/qzzy/p/urlpost.html
Copyright © 2011-2022 走看看