一个封装好的Asp.Net C# HttpRequest请求类

HTTP请求类:
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
View Code 
    
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Net;
using  System.IO;
    
namespace  WebRequestAPP
{
     ///
     /// Http请求
     ///
     public  class  HttpUtility
     {
         ///
         ///通用HttpWebRequest
         ///
         /// 请求方式(POST/GET)
         /// 请求地址
         /// 请求参数
         /// 完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)
         /// 请求返回的结果
         public  static  string Request(string method, string url, string param, Action onComplete = null)
         {
             if (string.IsNullOrEmpty(url))
              throw  new  ArgumentNullException( "URL" );
    
             switch  (method.ToUpper())
            
                 case  "POST":
                     return  Post(url, param, onComplete);
                 case  "GET":
                     return  Get(url, param, onComplete);
                 default:
                     throw  new  EntryPointNotFoundException( "method:"  + method);
             }
         }
    
         ///
         /// Post请求
         ///
         /// 请求地址
         /// 参数
         /// 完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)
         /// 请求返回的结果
         public  static  string Post(string url, string param, Action onComplete = null)
         {
             UrlCheck(ref url);
    
             byte[] bufferBytes = Encoding.UTF8.GetBytes(param);
    
             var request = WebRequest.Create(url) as HttpWebRequest; //HttpWebRequest方法继承自WebRequest, Create方法在WebRequest中定义,因此此处要显示的转换
             request.Method =  "POST" ;
             request.ContentLength = bufferBytes.Length;
             request.ContentType =  "application/x-www-form-urlencoded" ;
    
             try
             {
                 using  (var requestStream = request.GetRequestStream())
                 {
                     requestStream.Write(bufferBytes, 0, bufferBytes.Length);
                 }
             }
             catch  (WebException ex)
             {
    
                 return  ex.Message;
             }
    
             return  HttpRequest(request, onComplete);
         }
    
         ///
         /// GET请求
         ///
         /// 请求地址
         /// 参数
         /// 完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)
         /// 请求返回结果
         public  static  string Get(string url, string param, Action onComplete = null)
         {
             UrlCheck(ref url);
    
             if  (!string.IsNullOrEmpty(param))
                 if  (!param.StartsWith( "?" ))
                     param +=  "?"  + param;
                 else
                     param += param;
    
             var request = WebRequest.Create(url) as HttpWebRequest;
             request.Method =  "GET" ;
             request.ContentType =  "application/x-www-form-urlencoded" ;
    
             return  HttpRequest(request, onComplete);
         }
    
         ///
         /// 请求的主体部分(由此完成请求并返回请求结果)
         ///
         /// 请求的对象
         /// 完成后执行的操作(可选参数,通过此方法可以获取到HTTP状态码)
         /// 请求返回结果
         private  static  string HttpRequest(HttpWebRequest request, Action onComplete = null)
         {
             HttpWebResponse response = null;
    
             try
             {
                 response = request.GetResponse() as HttpWebResponse;
             }
             catch  (WebException ex)
             {
                 response = ex.Response as HttpWebResponse;
             }
    
             if  (response == null)
             {
                 if  (onComplete != null)
                     onComplete(HttpStatusCode.NotFound,  "请求远程返回为空" );
                 return  null;
             }
    
             string result = string.Empty;
             using  (StreamReader reader =  new  StreamReader(response.GetResponseStream()))
             {
                 result = reader.ReadToEnd();
             }
    
             if  (onComplete != null)
                 onComplete(response.StatusCode, result);
    
             return  result;
    
         }
    
         ///
         /// URL拼写完整性检查
         ///
         /// 待检查的URL
         private  static  void  UrlCheck(ref string url)
         {
             if  (!url.StartsWith( "http://" ) && !url.StartsWith( "https://" ))
                 url= "http://" +url;
         }
     }
}
http://www.dedecms.com/knowledge/program/asp-donet/2012/0803/4214.html

你可能感兴趣的:(.net)