zoukankan      html  css  js  c++  java
  • Post流提交、接收

    一、提交

     1 public void PostData(string Data, string cUrl)
     2         {
     3             HttpWebRequest myReq = null;
     4             HttpWebResponse response = null
     5             try
     6             {
     7                 byte[] arrB = Encoding.UTF8.GetBytes(Data);
     8                 myReq = (HttpWebRequest)WebRequest.Create(cUrl); //根据URL 创建对象
     9                 myReq.Method = "POST";
    10                 myReq.ContentType = "application/x-www-form-urlencoded";
    11                 myReq.ContentLength = arrB.Length;
    12                 Stream outStream = myReq.GetRequestStream();
    13                 outStream.Write(arrB, 0, arrB.Length);
    14                 outStream.Close();
    15 
    16                 response = (HttpWebResponse)myReq.GetResponse();
    17                 HttpStatusCode status = response.StatusCode;
    18                 switch (status)
    19                 {
    20                     case HttpStatusCode.OK: //返回状态码 200, 表示正常接收
    21                         tryTimes = 0//发送成功,将重试次数计数器清零
    22 
    23                         LogOrderSyncInfo(Data);//向数据库中记录订单推送日志
    24 
    25                         Log.WriteCommonLog("info""推送数据成功!");
    26                         break;
    27                     default//返回其他状态码,表示接收异常,需要重试3次
    28 
    29                         Thread.Sleep(3000);//失败后,过3秒后重试
    30 
    31                         if (tryTimes < 3)
    32                         {
    33                             tryTimes++;
    34                             Log.WriteCommonLog("error""推送数据出错!错误信息:" + status.ToString() + "正在进行第:" + tryTimes.ToString() +
    35                                                "次重试");
    36                             PostData(Data, cUrl);
    37                         }
    38                         else if (tryTimes == 3)
    39                         {
    40                             tryTimes = 0//发送失败,将重试次数计数器清零
    41                             //连续失败三次,则取消该订单推送
    42                         }
    43 
    44                         break;
    45                 }
    46             }
    47             catch (WebException ex) //表示接收异常,需要重试3次
    48             {
    49                 Thread.Sleep(3000);//失败后,过3秒后重试
    50 
    51                 if (tryTimes < 3)
    52                 {
    53                     tryTimes++;
    54                     Log.WriteCommonLog("error""推送数据出错!错误信息:" + ex.ToString() + "正在进行第:" + tryTimes.ToString() + "次重试");
    55                     PostData(Data, cUrl);
    56                 }
    57                 else if (tryTimes == 3)
    58                 {
    59                     tryTimes = 0//发送失败,将重试次数计数器清零
    60                     //连续失败三次,则取消该推送
    61                 }
    62             }
    63             finally
    64             {
    65                 if (myReq != null)
    66                     myReq.Abort();
    67                 if (response != null)
    68                     response.Close();              
    69             }

    70         } 

    二、接收 

    数据接收方法比较简单,我这里用的是ASP.NET MVC 

     1 [AcceptVerbs(HttpVerbs.Post)]
     2         public ActionResult Receive()
     3         {
     4             int result = 0;
     5             string msg = string.Empty;
     6             string cXml = string.Empty;
     7 
     8             try
     9             {
    10 
    11                 //1.获取商户推送过来的订单发货数据
    12                 //1.1 获取数据
    13                 StreamReader myStreamReader = new StreamReader((Stream)this.Request.InputStream);
    14                 cXml = myStreamReader.ReadToEnd();
    15 
    16                 result = 1;//DataOperate(cXml);//数据处理方法,返回处理结果
    17 
    18             }
    19             catch (Exception ex)
    20             {
    21                 result = 0;
    22                 msg = "出错,错误信息:" + ex.ToString();
    23                 Log.WriteCommonLog("error", msg);
    24             }
    25 
    26 
    27             if (result > 0)
    28                 Response.StatusCode = 200;
    29             else
    30                 Response.StatusCode = 404;
    31             return View();

    32         } 

  • 相关阅读:
    python 数字格式化
    Python字符串
    Nginx 深入-动静分离, 静态资源css, js 前端请求404问题
    Spring colud gateway 源码小计
    Nginx 场景应用
    Nginx valid_referer 防盗链
    Nginx 基础
    JNI 从零开始一次DEMO调用 IDEA 2018.2.5 + visual studio 2019
    Bitmap 图片说明
    HP激光打印机解密
  • 原文地址:https://www.cnblogs.com/xpengfee/p/2690499.html
Copyright © 2011-2022 走看看