博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Post流提交、接收
阅读量:4509 次
发布时间:2019-06-08

本文共 2413 字,大约阅读时间需要 8 分钟。

一、提交

 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         } 

转载于:https://www.cnblogs.com/xpengfee/archive/2012/09/18/2690499.html

你可能感兴趣的文章
关于IE6的一些需求分析
查看>>
【IPv6】ISATAP隧道技术详解
查看>>
numpy_pandas
查看>>
oracle数据导入/导出(2)
查看>>
SSH远程会话管理工具 - screen使用教程
查看>>
设计模式
查看>>
前端复习-01-dom操作包括ie和现代浏览器处理相关
查看>>
[CF612D] The Union of k-Segments(排序,扫描线)
查看>>
linux安装nginx
查看>>
spark书籍视频推荐
查看>>
django之富文本编辑器
查看>>
jsp第三章
查看>>
Android平台下利用zxing实现二维码开发
查看>>
【HTTP】Fiddler(三)- Fiddler命令行和HTTP断点调试
查看>>
镜像源归类
查看>>
IE下的document.onclick问题
查看>>
[模板]后缀数组
查看>>
git添加本地文件到github仓库
查看>>
0502《构建之法》第六、七章读后感
查看>>
[福大软工] Z班——Beta现场答辩反馈
查看>>