惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Jina AI
Jina AI
月光博客
月光博客
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
量子位
博客园 - 【当耐特】
The Cloudflare Blog
宝玉的分享
宝玉的分享
博客园 - 聂微东
博客园 - 叶小钗
美团技术团队
G
Google Developers Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
小众软件
小众软件

博客园 - xpengfee

【转】Quartz.NET 【转】阿里巴巴分布式服务框架 Dubbo 团队成员梁飞专访 【转】关于RabbitMQ 【转】windows下nginx+mono+fastCGI部署asp.net网站 【转】玩玩负载均衡---在window与linux下配置nginx 【转】亿级Web系统搭建——单机到分布式集群 【转】集群和负载均衡的概念 【转】SQL Server 2008 新数据类型 Dokuwiki 【转】反向AJAX 【转】Asp.net MVC Comet推送 【转】Comet:基于 HTTP 长连接的“服务器推”技术 【转】WEB前端调优 【转】jquery 注册事件的方法 【转】Google Chrome浏览器调试 【转】ASP.NET MVC 4 RC的JS/CSS打包压缩功能 【转】SQLServer连接字符串配置:MultipleActiveResultSets VS妙用--自动创建文件注释头 原型设计工具Axure RP分享
Post流提交、接收
xpengfee · 2012-09-18 · via 博客园 - xpengfee

一、提交

 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         }