11using System ;
22using System . Collections . Generic ;
3+ using System . Collections . Specialized ;
34using System . Text ;
45using System . IO ;
56using System . Net ;
@@ -60,13 +61,46 @@ public static string CreateFormDataBoundary()
6061 }
6162
6263 /// <summary>
63- /// HTTP-GET方法
64+ /// HTTP-GET 方法(不包含 headers)
6465 /// </summary>
65- /// <param name="url">请求目标URL </param>
66- /// <param name="token">令牌(凭证)[可选->设置为null ]</param>
66+ /// <param name="url">请求目标 URL </param>
67+ /// <param name="token">令牌(凭证)[可选 -> 设置为 null ]</param>
6768 /// <param name="binaryMode">是否以二进制模式读取响应内容(默认:否,即表示以文本方式读取)</param>
6869 /// <returns>HTTP-GET的响应结果</returns>
6970 public HttpResult Get ( string url , string token , bool binaryMode = false )
71+ {
72+ return Get ( url , null , token , binaryMode ) ;
73+ }
74+
75+ public HttpResult Get ( string url , StringDictionary headers , Auth auth , bool binaryMode = false )
76+ {
77+ if ( headers == null )
78+ {
79+ headers = new StringDictionary {
80+ { "Content-Type" , ContentType . WWW_FORM_URLENC }
81+ } ;
82+ }
83+
84+ if ( ! headers . ContainsKey ( "Content-Type" ) )
85+ {
86+ headers [ "Content-Type" ] = ContentType . WWW_FORM_URLENC ;
87+ }
88+
89+ addAuthHeaders ( ref headers , auth ) ;
90+
91+ string token = auth . CreateManageTokenV2 ( "GET" , url , headers ) ;
92+ return Get ( url , headers , token , binaryMode ) ;
93+ }
94+
95+ /// <summary>
96+ /// HTTP-GET 方法
97+ /// </summary>
98+ /// <param name="url">请求目标URL</param>
99+ /// <param name="headers">请求 Headers[可选 -> 设置为 null]</param>
100+ /// <param name="token">令牌(凭证)[可选 -> 设置为 null]</param>
101+ /// <param name="binaryMode">是否以二进制模式读取响应内容(默认:否,即表示以文本方式读取)</param>
102+ /// <returns>HTTP-GET的响应结果</returns>
103+ public HttpResult Get ( string url , StringDictionary headers , string token , bool binaryMode = false )
70104 {
71105 HttpResult result = new HttpResult ( ) ;
72106
@@ -76,6 +110,18 @@ public HttpResult Get(string url, string token, bool binaryMode = false)
76110 {
77111 wReq = WebRequest . Create ( url ) as HttpWebRequest ;
78112 wReq . Method = "GET" ;
113+ if ( headers != null )
114+ {
115+ foreach ( string fieldName in headers . Keys )
116+ {
117+ wReq . Headers . Add ( fieldName , headers [ fieldName ] ) ;
118+ }
119+
120+ if ( headers . ContainsKey ( "Content-Type" ) )
121+ {
122+ wReq . ContentType = headers [ "Content-Type" ] ;
123+ }
124+ }
79125 if ( ! string . IsNullOrEmpty ( token ) )
80126 {
81127 wReq . Headers . Add ( "Authorization" , token ) ;
@@ -165,13 +211,46 @@ public HttpResult Get(string url, string token, bool binaryMode = false)
165211 }
166212
167213 /// <summary>
168- /// HTTP-POST方法(不包含body数据)
214+ /// HTTP-POST 方法(不包含 headers,不包含 body 数据)
169215 /// </summary>
170- /// <param name="url">请求目标URL </param>
171- /// <param name="token">令牌(凭证)[可选]</param>
216+ /// <param name="url">请求目标 URL </param>
217+ /// <param name="token">令牌(凭证)[可选 -> 设置为 null ]</param>
172218 /// <param name="binaryMode">是否以二进制模式读取响应内容(默认:否,即表示以文本方式读取)</param>
173- /// <returns>HTTP-POST的响应结果 </returns>
219+ /// <returns>HTTP-POST 的响应结果 </returns>
174220 public HttpResult Post ( string url , string token , bool binaryMode = false )
221+ {
222+ return Post ( url , null , token , binaryMode ) ;
223+ }
224+
225+ public HttpResult Post ( string url , StringDictionary headers , Auth auth , bool binaryMode = false )
226+ {
227+ if ( headers == null )
228+ {
229+ headers = new StringDictionary {
230+ { "Content-Type" , ContentType . WWW_FORM_URLENC }
231+ } ;
232+ }
233+
234+ if ( ! headers . ContainsKey ( "Content-Type" ) )
235+ {
236+ headers [ "Content-Type" ] = ContentType . WWW_FORM_URLENC ;
237+ }
238+
239+ addAuthHeaders ( ref headers , auth ) ;
240+
241+ string token = auth . CreateManageTokenV2 ( "POST" , url , headers ) ;
242+ return Post ( url , headers , token , binaryMode ) ;
243+ }
244+
245+ /// <summary>
246+ /// HTTP-POST 方法(不包含 body 数据)
247+ /// </summary>
248+ /// <param name="url">请求目标 URL</param>
249+ /// <param name="token">令牌(凭证)[可选 -> 设置为 null]</param>
250+ /// <param name="headers">请求 Headers[可选 -> 设置为 null]</param>
251+ /// <param name="binaryMode">是否以二进制模式读取响应内容(默认:否,即表示以文本方式读取)</param>
252+ /// <returns>HTTP-POST 的响应结果</returns>
253+ public HttpResult Post ( string url , StringDictionary headers , string token , bool binaryMode = false )
175254 {
176255 HttpResult result = new HttpResult ( ) ;
177256
@@ -181,6 +260,18 @@ public HttpResult Post(string url, string token, bool binaryMode = false)
181260 {
182261 wReq = WebRequest . Create ( url ) as HttpWebRequest ;
183262 wReq . Method = "POST" ;
263+ if ( headers != null )
264+ {
265+ foreach ( string fieldName in headers . Keys )
266+ {
267+ wReq . Headers . Add ( fieldName , headers [ fieldName ] ) ;
268+ }
269+
270+ if ( headers . ContainsKey ( "Content-Type" ) )
271+ {
272+ wReq . ContentType = headers [ "Content-Type" ] ;
273+ }
274+ }
184275 if ( ! string . IsNullOrEmpty ( token ) )
185276 {
186277 wReq . Headers . Add ( "Authorization" , token ) ;
@@ -978,6 +1069,26 @@ public HttpResult PostForm(string url, string data, string token, bool binaryMod
9781069 return result ;
9791070 }
9801071
1072+ public HttpResult PostForm ( string url , StringDictionary headers , string data , Auth auth , bool binaryMode = false )
1073+ {
1074+ if ( headers == null )
1075+ {
1076+ headers = new StringDictionary {
1077+ { "Content-Type" , ContentType . WWW_FORM_URLENC }
1078+ } ;
1079+ }
1080+
1081+ if ( ! headers . ContainsKey ( "Content-Type" ) )
1082+ {
1083+ headers [ "Content-Type" ] = ContentType . WWW_FORM_URLENC ;
1084+ }
1085+
1086+ addAuthHeaders ( ref headers , auth ) ;
1087+
1088+ string token = auth . CreateManageTokenV2 ( "POST" , url , headers , data ) ;
1089+ return PostForm ( url , headers , Encoding . UTF8 . GetBytes ( data ) , token , binaryMode ) ;
1090+ }
1091+
9811092 /// <summary>
9821093 /// HTTP-POST方法(包含表单数据)
9831094 /// </summary>
@@ -987,6 +1098,20 @@ public HttpResult PostForm(string url, string data, string token, bool binaryMod
9871098 /// <param name="binaryMode">是否以二进制模式读取响应内容(默认:否,即表示以文本方式读取)</param>
9881099 /// <returns>HTTP-POST的响应结果</returns>
9891100 public HttpResult PostForm ( string url , byte [ ] data , string token , bool binaryMode = false )
1101+ {
1102+ return PostForm ( url , null , data , token , binaryMode ) ;
1103+ }
1104+
1105+ /// <summary>
1106+ /// HTTP-POST方法(包含表单数据)
1107+ /// </summary>
1108+ /// <param name="url">请求目标URL</param>
1109+ /// <param name="headers">请求 Headers[可选 -> 设置为 null]</param>
1110+ /// <param name="data">表单数据</param>
1111+ /// <param name="token">令牌(凭证)[可选->设置为null]</param>
1112+ /// <param name="binaryMode">是否以二进制模式读取响应内容(默认:否,即表示以文本方式读取)</param>
1113+ /// <returns>HTTP-POST的响应结果</returns>
1114+ public HttpResult PostForm ( string url , StringDictionary headers , byte [ ] data , string token , bool binaryMode = false )
9901115 {
9911116 HttpResult result = new HttpResult ( ) ;
9921117
@@ -996,6 +1121,13 @@ public HttpResult PostForm(string url, byte[] data, string token, bool binaryMod
9961121 {
9971122 wReq = WebRequest . Create ( url ) as HttpWebRequest ;
9981123 wReq . Method = "POST" ;
1124+ if ( headers != null )
1125+ {
1126+ foreach ( string fieldName in headers . Keys )
1127+ {
1128+ wReq . Headers . Add ( fieldName , headers [ fieldName ] ) ;
1129+ }
1130+ }
9991131 if ( ! string . IsNullOrEmpty ( token ) )
10001132 {
10011133 wReq . Headers . Add ( "Authorization" , token ) ;
@@ -1380,5 +1512,29 @@ private void getHeaders(ref HttpResult hr, HttpWebResponse resp)
13801512 }
13811513 }
13821514
1515+
1516+ private void addAuthHeaders ( ref StringDictionary headers , Auth auth )
1517+ {
1518+ string xQiniuDate = DateTime . UtcNow . ToString ( "yyyyMMdd'T'HHmmss'Z'" ) ;
1519+ string xQiniuDateDisableEnv = Environment . GetEnvironmentVariable ( "DISABLE_QINIU_TIMESTAMP_SIGNATURE" ) ;
1520+ if ( auth . AuthOptions . DisableQiniuTimestampSignature is bool disableQiniuTimestampSignature )
1521+ {
1522+ if ( ! disableQiniuTimestampSignature )
1523+ {
1524+ headers [ "X-Qiniu-Date" ] = xQiniuDate ;
1525+ }
1526+ }
1527+ else if ( ! String . IsNullOrEmpty ( xQiniuDateDisableEnv ) )
1528+ {
1529+ if ( xQiniuDateDisableEnv . ToLower ( ) != "true" )
1530+ {
1531+ headers [ "X-Qiniu-Date" ] = xQiniuDate ;
1532+ }
1533+ }
1534+ else
1535+ {
1536+ headers [ "X-Qiniu-Date" ] = xQiniuDate ;
1537+ }
1538+ }
13831539 }
13841540}
0 commit comments